forked from wenj91/gobatis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gobatis.go
356 lines (292 loc) · 7.81 KB
/
gobatis.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package gobatis
import (
"context"
"database/sql"
"errors"
)
type ResultType string
var LOG ILogger = defLog
const (
resultTypeMap ResultType = "map" // result set is a map: map[string]interface{}
resultTypeMaps ResultType = "maps" // result set is a slice, item is map: []map[string]interface{}
resultTypeStruct ResultType = "struct" // result set is a struct
resultTypeStructs ResultType = "structs" // result set is a slice, item is struct
resultTypeSlice ResultType = "slice" // result set is a value slice, []interface{}
resultTypeSlices ResultType = "slices" // result set is a value slice, item is value slice, []interface{}
resultTypeArray ResultType = "array" //
resultTypeArrays ResultType = "arrays" // result set is a value slice, item is value slice, []interface{}
resultTypeValue ResultType = "value" // result set is single value
)
type GoBatis interface {
// Select 查询数据
Select(stmt string, param interface{}) func(res interface{}) error
// SelectContext 查询数据with context
SelectContext(ctx context.Context, stmt string, param interface{}) func(res interface{}) error
// Insert 插入数据
Insert(stmt string, param interface{}) (int64, int64, error)
// InsertContext 插入数据with context
InsertContext(ctx context.Context, stmt string, param interface{}) (int64, int64, error)
// Update 更新数据
Update(stmt string, param interface{}) (int64, error)
// UpdateContext 更新数据with context
UpdateContext(ctx context.Context, stmt string, param interface{}) (int64, error)
// Delete 刪除数据
Delete(stmt string, param interface{}) (int64, error)
// DeleteContext 刪除数据with context
DeleteContext(ctx context.Context, stmt string, param interface{}) (int64, error)
}
// reference from https://github.com/yinshuwei/osm/blob/master/osm.go start
type dbRunner interface {
Prepare(query string) (*sql.Stmt, error)
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
Exec(query string, args ...interface{}) (sql.Result, error)
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
Query(query string, args ...interface{}) (*sql.Rows, error)
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryRow(query string, args ...interface{}) *sql.Row
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}
func Get(datasource string) *DB {
if nil == conf {
panic(errors.New("DB config no init, please invoke DB.ConfInit() to init db config!"))
}
if nil == db {
panic(errors.New("DB init err, db == nil!"))
}
ds, ok := db[datasource]
if !ok {
panic(errors.New("Datasource:" + datasource + " not exists!"))
}
dbType := ds.dbType
if dbType != DBTypeMySQL {
panic(errors.New("No support to this driver name!"))
}
gb := &DB{
gbBase{
db: ds.db,
dbType: ds.dbType,
config: conf,
},
}
return gb
}
func SetLogger(log ILogger) {
LOG = log
}
type gbBase struct {
db dbRunner
dbType DBType
config *config
}
// DB
type DB struct {
gbBase
}
var _ GoBatis = &DB{}
// TX
type TX struct {
gbBase
}
var _ GoBatis = &TX{}
// Begin TX
//
// ps:
// TX, err := this.Begin()
func (d *DB) Begin() (*TX, error) {
if nil == d.db {
return nil, errors.New("db no opened")
}
sqlDB, ok := d.db.(*sql.DB)
if !ok {
return nil, errors.New("db no opened")
}
db, err := sqlDB.Begin()
if nil != err {
return nil, err
}
t := &TX{
gbBase{
dbType: d.dbType,
config: d.config,
db: db,
},
}
return t, nil
}
// Begin TX with ctx & opts
//
// ps:
// TX, err := this.BeginTx(ctx, ops)
func (d *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*TX, error) {
if nil == d.db {
return nil, errors.New("db no opened")
}
sqlDb, ok := d.db.(*sql.DB)
if !ok {
return nil, errors.New("db no opened")
}
db, err := sqlDb.BeginTx(ctx, opts)
if nil != err {
return nil, err
}
t := &TX{
gbBase{
dbType: d.dbType,
config: d.config,
db: db,
},
}
return t, nil
}
// Close db
//
// ps:
// err := this.Close()
func (g *gbBase) Close() error {
if nil == g.db {
return errors.New("db no opened")
}
sqlDb, ok := g.db.(*sql.DB)
if !ok {
return errors.New("db no opened")
}
err := sqlDb.Close()
g.db = nil
return err
}
// Commit TX
//
// ps:
// err := TX.Commit()
func (t *TX) Commit() error {
if nil == t.db {
return errors.New("TX no running")
}
sqlTx, ok := t.db.(*sql.Tx)
if !ok {
return errors.New("TX no running")
}
return sqlTx.Commit()
}
// Rollback TX
//
// ps:
// err := TX.Rollback()
func (t *TX) Rollback() error {
if nil == t.db {
return errors.New("TX no running")
}
sqlTx, ok := t.db.(*sql.Tx)
if !ok {
return errors.New("TX no running")
}
return sqlTx.Rollback()
}
// reference from https://github.com/yinshuwei/osm/blob/master/osm.go end
func (g *gbBase) Select(stmt string, param interface{}) func(res interface{}) error {
ms := g.config.mapperConf.getMappedStmt(stmt)
if nil == ms {
return func(res interface{}) error {
return errors.New("Mapped statement not found:" + stmt)
}
}
ms.dbType = g.dbType
params := paramProcess(param)
return func(res interface{}) error {
executor := &executor{
gb: g,
}
err := executor.query(ms, params, res)
return err
}
}
func (g *gbBase) SelectContext(ctx context.Context, stmt string, param interface{}) func(res interface{}) error {
ms := g.config.mapperConf.getMappedStmt(stmt)
if nil == ms {
return func(res interface{}) error {
return errors.New("Mapped statement not found:" + stmt)
}
}
ms.dbType = g.dbType
params := paramProcess(param)
return func(res interface{}) error {
executor := &executor{
gb: g,
}
err := executor.queryContext(ctx, ms, params, res)
return err
}
}
// insert(stmt string, param interface{})
func (g *gbBase) Insert(stmt string, param interface{}) (int64, int64, error) {
ms := g.config.mapperConf.getMappedStmt(stmt)
if nil == ms {
return 0, 0, errors.New("Mapped statement not found:" + stmt)
}
ms.dbType = g.dbType
params := paramProcess(param)
executor := &executor{
gb: g,
}
lastInsertId, affected, err := executor.update(ms, params)
if nil != err {
return 0, 0, err
}
return lastInsertId, affected, nil
}
func (g *gbBase) InsertContext(ctx context.Context, stmt string, param interface{}) (int64, int64, error) {
ms := g.config.mapperConf.getMappedStmt(stmt)
if nil == ms {
return 0, 0, errors.New("Mapped statement not found:" + stmt)
}
ms.dbType = g.dbType
params := paramProcess(param)
executor := &executor{
gb: g,
}
lastInsertId, affected, err := executor.updateContext(ctx, ms, params)
if nil != err {
return 0, 0, err
}
return lastInsertId, affected, nil
}
// update(stmt string, param interface{})
func (g *gbBase) Update(stmt string, param interface{}) (int64, error) {
ms := g.config.mapperConf.getMappedStmt(stmt)
if nil == ms {
return 0, errors.New("Mapped statement not found:" + stmt)
}
ms.dbType = g.dbType
params := paramProcess(param)
executor := &executor{
gb: g,
}
_, affected, err := executor.update(ms, params)
if nil != err {
return 0, err
}
return affected, nil
}
func (g *gbBase) UpdateContext(ctx context.Context, stmt string, param interface{}) (int64, error) {
ms := g.config.mapperConf.getMappedStmt(stmt)
if nil == ms {
return 0, errors.New("Mapped statement not found:" + stmt)
}
ms.dbType = g.dbType
params := paramProcess(param)
executor := &executor{
gb: g,
}
_, affected, err := executor.updateContext(ctx, ms, params)
if nil != err {
return 0, err
}
return affected, nil
}
// delete(stmt string, param interface{})
func (g *gbBase) Delete(stmt string, param interface{}) (int64, error) {
return g.Update(stmt, param)
}
func (g *gbBase) DeleteContext(ctx context.Context, stmt string, param interface{}) (int64, error) {
return g.UpdateContext(ctx, stmt, param)
}