@@ -7,14 +7,13 @@ import (
7
7
"goAdmin/config"
8
8
)
9
9
10
- type sqlTx struct {
10
+ type SqlTxStruct struct {
11
11
Tx * sql.Tx
12
12
}
13
13
14
14
var (
15
15
sqlDBmap map [string ]* sql.DB
16
- SqlDB * sql.DB
17
- SqlTx sqlTx
16
+ SqlDB * sql.DB
18
17
)
19
18
20
19
// 只会执行一次在执行程序启动的时候
@@ -40,6 +39,62 @@ func InitDB(username string, password string, port string, ip string, databaseNa
40
39
}
41
40
}
42
41
42
+ func QueryWithConnection (con string , query string , args ... interface {}) ([]map [string ]interface {}, * sql.Rows ) {
43
+
44
+ rs , err := sqlDBmap [con ].Query (query , args ... )
45
+
46
+ if err != nil {
47
+ if rs != nil {
48
+ rs .Close ()
49
+ }
50
+ panic (err )
51
+ }
52
+
53
+ col , colErr := rs .Columns ()
54
+
55
+ if colErr != nil {
56
+ if rs != nil {
57
+ rs .Close ()
58
+ }
59
+ panic (colErr )
60
+ }
61
+
62
+ typeVal , err := rs .ColumnTypes ()
63
+ if err != nil {
64
+ if rs != nil {
65
+ rs .Close ()
66
+ }
67
+ panic (err )
68
+ }
69
+
70
+ results := make ([]map [string ]interface {}, 0 )
71
+
72
+ for rs .Next () {
73
+ var colVar = make ([]interface {}, len (col ))
74
+ for i := 0 ; i < len (col ); i ++ {
75
+ SetColVarType (& colVar , i , typeVal [i ].DatabaseTypeName ())
76
+ }
77
+ result := make (map [string ]interface {})
78
+ if scanErr := rs .Scan (colVar ... ); scanErr != nil {
79
+ rs .Close ()
80
+ panic (scanErr )
81
+ }
82
+ for j := 0 ; j < len (col ); j ++ {
83
+ SetResultValue (& result , col [j ], colVar [j ], typeVal [j ].DatabaseTypeName ())
84
+ }
85
+ results = append (results , result )
86
+ }
87
+ if err := rs .Err (); err != nil {
88
+ if rs != nil {
89
+ rs .Close ()
90
+ }
91
+ panic (err )
92
+ }
93
+ rs .Close ()
94
+ return results , rs
95
+ }
96
+
97
+
43
98
func Query (query string , args ... interface {}) ([]map [string ]interface {}, * sql.Rows ) {
44
99
45
100
rs , err := sqlDBmap ["default" ].Query (query , args ... )
@@ -104,25 +159,50 @@ func Exec(query string, args ...interface{}) sql.Result {
104
159
return rs
105
160
}
106
161
107
- func BeginTransactions () * sqlTx {
162
+ func BeginTransactionsByLevel () * SqlTxStruct {
163
+
164
+ //LevelDefault IsolationLevel = iota
165
+ //LevelReadUncommitted
166
+ //LevelReadCommitted
167
+ //LevelWriteCommitted
168
+ //LevelRepeatableRead
169
+ //LevelSnapshot
170
+ //LevelSerializable
171
+ //LevelLinearizable
172
+
173
+ SqlTx := new (SqlTxStruct )
174
+
175
+ tx , err := SqlDB .BeginTx (context .Background (),
176
+ & sql.TxOptions {Isolation : sql .LevelReadUncommitted })
177
+ if err != nil {
178
+ panic (err )
179
+ }
180
+ (* SqlTx ).Tx = tx
181
+ return SqlTx
182
+ }
183
+
184
+ func BeginTransactions () * SqlTxStruct {
108
185
tx , err := SqlDB .BeginTx (context .Background (),
109
186
& sql.TxOptions {Isolation : sql .LevelDefault })
110
187
if err != nil {
111
188
panic (err )
112
189
}
113
- SqlTx .Tx = tx
114
- return & SqlTx
190
+
191
+ SqlTx := new (SqlTxStruct )
192
+
193
+ (* SqlTx ).Tx = tx
194
+ return SqlTx
115
195
}
116
196
117
- func (SqlTx * sqlTx ) Exec (query string , args ... interface {}) (sql.Result , error ) {
197
+ func (SqlTx * SqlTxStruct ) Exec (query string , args ... interface {}) (sql.Result , error ) {
118
198
rs , err := SqlTx .Tx .Exec (query , args ... )
119
199
if err != nil {
120
200
return nil , err
121
201
}
122
202
return rs , nil
123
203
}
124
204
125
- func (SqlTx * sqlTx ) Query (query string , args ... interface {}) ([]map [string ]interface {}, error ) {
205
+ func (SqlTx * SqlTxStruct ) Query (query string , args ... interface {}) ([]map [string ]interface {}, error ) {
126
206
rs , err := SqlTx .Tx .Query (query , args ... )
127
207
128
208
if err != nil {
@@ -145,7 +225,7 @@ func (SqlTx *sqlTx) Query(query string, args ...interface{}) ([]map[string]inter
145
225
results := make ([]map [string ]interface {}, 0 )
146
226
147
227
for rs .Next () {
148
- var colVar = make ([]interface {}, len (col ))
228
+ var colVar = make ([]interface {}, len (col ))
149
229
for i := 0 ; i < len (col ); i ++ {
150
230
SetColVarType (& colVar , i , typeVal [i ].DatabaseTypeName ())
151
231
}
@@ -166,7 +246,31 @@ func (SqlTx *sqlTx) Query(query string, args ...interface{}) ([]map[string]inter
166
246
return results , nil
167
247
}
168
248
169
- func SetColVarType (colVar * []interface {}, i int , typeName string ) {
249
+ type TxFn func (* SqlTxStruct ) (error , map [string ]interface {})
250
+
251
+ func WithTransaction (fn TxFn ) (err error , res map [string ]interface {}) {
252
+
253
+ SqlTx := BeginTransactions ()
254
+
255
+ defer func () {
256
+ if p := recover (); p != nil {
257
+ // a panic occurred, rollback and repanic
258
+ SqlTx .Tx .Rollback ()
259
+ panic (p )
260
+ } else if err != nil {
261
+ // something went wrong, rollback
262
+ SqlTx .Tx .Rollback ()
263
+ } else {
264
+ // all good, commit
265
+ err = SqlTx .Tx .Commit ()
266
+ }
267
+ }()
268
+
269
+ err , res = fn (SqlTx )
270
+ return
271
+ }
272
+
273
+ func SetColVarType (colVar * []interface {}, i int , typeName string ) {
170
274
switch typeName {
171
275
case "INT" :
172
276
var s sql.NullInt64
@@ -228,7 +332,7 @@ func SetColVarType(colVar *[]interface{}, i int, typeName string) {
228
332
}
229
333
}
230
334
231
- func SetResultValue (result * map [string ]interface {}, index string , colVar interface {}, typeName string ) {
335
+ func SetResultValue (result * map [string ]interface {}, index string , colVar interface {}, typeName string ) {
232
336
switch typeName {
233
337
case "INT" :
234
338
temp := * (colVar .(* sql.NullInt64 ))
@@ -360,4 +464,4 @@ func SetResultValue(result *map[string]interface{}, index string, colVar interfa
360
464
default :
361
465
(* result )[index ] = colVar
362
466
}
363
- }
467
+ }
0 commit comments