Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 56a7910

Browse files
author
cg33
committedAug 1, 2018
fixed: mysql trans bug
1 parent 14153ab commit 56a7910

File tree

4 files changed

+217
-14
lines changed

4 files changed

+217
-14
lines changed
 

‎connections/mysql/mysql.go

+116-12
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ import (
77
"goAdmin/config"
88
)
99

10-
type sqlTx struct {
10+
type SqlTxStruct struct {
1111
Tx *sql.Tx
1212
}
1313

1414
var (
1515
sqlDBmap map[string]*sql.DB
16-
SqlDB *sql.DB
17-
SqlTx sqlTx
16+
SqlDB *sql.DB
1817
)
1918

2019
// 只会执行一次在执行程序启动的时候
@@ -40,6 +39,62 @@ func InitDB(username string, password string, port string, ip string, databaseNa
4039
}
4140
}
4241

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+
4398
func Query(query string, args ...interface{}) ([]map[string]interface{}, *sql.Rows) {
4499

45100
rs, err := sqlDBmap["default"].Query(query, args...)
@@ -104,25 +159,50 @@ func Exec(query string, args ...interface{}) sql.Result {
104159
return rs
105160
}
106161

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 {
108185
tx, err := SqlDB.BeginTx(context.Background(),
109186
&sql.TxOptions{Isolation: sql.LevelDefault})
110187
if err != nil {
111188
panic(err)
112189
}
113-
SqlTx.Tx = tx
114-
return &SqlTx
190+
191+
SqlTx := new(SqlTxStruct)
192+
193+
(*SqlTx).Tx = tx
194+
return SqlTx
115195
}
116196

117-
func (SqlTx *sqlTx) Exec(query string, args ...interface{}) (sql.Result, error) {
197+
func (SqlTx *SqlTxStruct) Exec(query string, args ...interface{}) (sql.Result, error) {
118198
rs, err := SqlTx.Tx.Exec(query, args...)
119199
if err != nil {
120200
return nil, err
121201
}
122202
return rs, nil
123203
}
124204

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) {
126206
rs, err := SqlTx.Tx.Query(query, args...)
127207

128208
if err != nil {
@@ -145,7 +225,7 @@ func (SqlTx *sqlTx) Query(query string, args ...interface{}) ([]map[string]inter
145225
results := make([]map[string]interface{}, 0)
146226

147227
for rs.Next() {
148-
var colVar = make([]interface{}, len(col))
228+
var colVar= make([]interface{}, len(col))
149229
for i := 0; i < len(col); i++ {
150230
SetColVarType(&colVar, i, typeVal[i].DatabaseTypeName())
151231
}
@@ -166,7 +246,31 @@ func (SqlTx *sqlTx) Query(query string, args ...interface{}) ([]map[string]inter
166246
return results, nil
167247
}
168248

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) {
170274
switch typeName {
171275
case "INT":
172276
var s sql.NullInt64
@@ -228,7 +332,7 @@ func SetColVarType(colVar *[]interface{}, i int, typeName string) {
228332
}
229333
}
230334

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) {
232336
switch typeName {
233337
case "INT":
234338
temp := *(colVar.(*sql.NullInt64))
@@ -360,4 +464,4 @@ func SetResultValue(result *map[string]interface{}, index string, colVar interfa
360464
default:
361465
(*result)[index] = colVar
362466
}
363-
}
467+
}

‎controllers/manager.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package controller
2+
3+
import "github.com/valyala/fasthttp"
4+
5+
func NewManager(ctx *fasthttp.RequestCtx) {
6+
7+
}
8+
9+
func DeleteManager(ctx *fasthttp.RequestCtx) {
10+
11+
}
12+
13+
func EditManager(ctx *fasthttp.RequestCtx) {
14+
15+
}
16+
17+
func ShowManager(ctx *fasthttp.RequestCtx) {
18+
19+
}
20+
21+
func NewManagerRules(ctx *fasthttp.RequestCtx) {
22+
23+
}
24+
25+
func DeleteManagerRules(ctx *fasthttp.RequestCtx) {
26+
27+
}
28+
29+
func EditManagerRules(ctx *fasthttp.RequestCtx) {
30+
31+
}
32+
33+
func ShowManagerRules(ctx *fasthttp.RequestCtx) {
34+
35+
}
36+
37+
func NewManagerRoles(ctx *fasthttp.RequestCtx) {
38+
39+
}
40+
41+
func DeleteManagerRoles(ctx *fasthttp.RequestCtx) {
42+
43+
}
44+
45+
func EditManagerRoles(ctx *fasthttp.RequestCtx) {
46+
47+
}
48+
49+
func ShowManagerRoles(ctx *fasthttp.RequestCtx) {
50+
51+
}

‎controllers/operation.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package controller
2+
3+
import "github.com/valyala/fasthttp"
4+
5+
func NewOperationLog(ctx *fasthttp.RequestCtx) {
6+
7+
}
8+
9+
func DeleteOperationLog(ctx *fasthttp.RequestCtx) {
10+
11+
}
12+
13+
func EditOperationLog(ctx *fasthttp.RequestCtx) {
14+
15+
}
16+
17+
func ShowOperationLog(ctx *fasthttp.RequestCtx) {
18+
19+
}

‎router.go

+31-2
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,56 @@ import (
1010
func InitRouter() *fasthttprouter.Router {
1111

1212
router := fasthttprouter.New()
13+
14+
// 授权认证
1315
router.GET("/login", controller.ShowLogin)
1416
router.POST("/signin", controller.Auth)
15-
router.GET("/install", controller.ShowInstall)
16-
1717
router.POST("/logout", AuthMiddleware(controller.Logout))
18+
19+
// 菜单管理
1820
router.GET("/menu", AuthMiddleware(controller.ShowMenu))
1921
router.POST("/menu/delete", AuthMiddleware(controller.DeleteMenu))
2022
router.POST("/menu/new", AuthMiddleware(controller.NewMenu))
2123
router.GET("/menu/new", AuthMiddleware(controller.ShowMenu))
2224
router.POST("/menu/edit", AuthMiddleware(controller.EditMenu))
2325
router.GET("/menu/edit/show", AuthMiddleware(controller.ShowEditMenu))
2426

27+
// 增删改查管理
2528
router.GET("/info/:prefix", AuthMiddleware(controller.ShowInfo))
2629
router.GET("/info/:prefix/edit", AuthMiddleware(controller.ShowForm))
2730
router.GET("/info/:prefix/new", AuthMiddleware(controller.ShowNewForm))
2831
router.POST("/edit/:prefix", AuthMiddleware(controller.EditForm))
2932
router.POST("/delete/:prefix", AuthMiddleware(controller.DeleteData))
3033
router.POST("/new/:prefix", AuthMiddleware(controller.NewForm))
3134

35+
// 自动化安装
36+
router.GET("/install", controller.ShowInstall)
3237
router.POST("/install/database/check", controller.CheckDatabase)
3338

39+
// 管理员管理
40+
router.GET("/manager/list", controller.ShowManager)
41+
router.POST("/manager/new", controller.NewManager)
42+
router.POST("/manager/delete", controller.DeleteManager)
43+
router.POST("/manager/edit", controller.EditManager)
44+
45+
// 管理权限管理
46+
router.GET("/manager/rules/list", controller.ShowManagerRules)
47+
router.POST("/manager/rules/new", controller.NewManagerRules)
48+
router.POST("/manager/rules/delete", controller.DeleteManagerRules)
49+
router.POST("/manager/rules/edit", controller.EditManagerRules)
50+
51+
// 管理角色管理
52+
router.GET("/manager/roles/list", controller.ShowManagerRoles)
53+
router.POST("/manager/roles/new", controller.NewManagerRoles)
54+
router.POST("/manager/roles/delete", controller.DeleteManagerRoles)
55+
router.POST("/manager/roles/edit", controller.EditManagerRoles)
56+
57+
// 操作日志管理
58+
router.GET("/operation/log/list", controller.ShowOperationLog)
59+
router.POST("/operation/log/new", controller.NewOperationLog)
60+
router.POST("/operation/log/delete", controller.DeleteOperationLog)
61+
router.POST("/operation/log/edit", controller.EditOperationLog)
62+
3463
return router
3564
}
3665

0 commit comments

Comments
 (0)
Please sign in to comment.