-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysqlTx.go
94 lines (87 loc) · 1.86 KB
/
mysqlTx.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
package mysql
import (
"database/sql"
"log"
)
//MyDbTx MyDbTx
type MyDbTx struct {
Tx *sql.Tx
}
//Insert Insert
func (t *MyDbTx) Insert(query string, args ...interface{}) (bool, int64) {
var success = false
var id int64 = -1
//var stmtIns *sql.Stmt
stmtIns, err := t.Tx.Prepare(query)
if err != nil {
log.Println("Insert transaction prepare err:", err.Error())
} else {
defer stmtIns.Close()
res, err := stmtIns.Exec(args...)
if err != nil {
log.Println("Insert transaction Exec err:", err.Error())
} else {
id, _ = res.LastInsertId()
success = true
}
}
return success, id
}
//Update Update
func (t *MyDbTx) Update(query string, args ...interface{}) bool {
var success = false
stmtIns, err := t.Tx.Prepare(query)
if err != nil {
log.Println("Update transaction prepare err:", err.Error())
} else {
defer stmtIns.Close()
_, err := stmtIns.Exec(args...)
if err != nil {
log.Println("Update transaction Exec err:", err.Error())
} else {
success = true
}
}
return success
}
//Delete Delete
func (t *MyDbTx) Delete(query string, args ...interface{}) bool {
var success = false
stmtIns, err := t.Tx.Prepare(query)
if err != nil {
log.Println("Delete transaction prepare err:", err.Error())
} else {
defer stmtIns.Close()
_, err := stmtIns.Exec(args...)
if err != nil {
log.Println("Delete transaction Exec err:", err.Error())
//t.Tx.Rollback()
} else {
success = true
//t.Tx.Commit()
}
}
return success
}
//Commit Commit
func (t *MyDbTx) Commit() bool {
var rtn = false
err := t.Tx.Commit()
if err != nil {
log.Println("Commit transaction err:", err.Error())
} else {
rtn = true
}
return rtn
}
//Rollback Rollback
func (t *MyDbTx) Rollback() bool {
var rtn = false
err := t.Tx.Rollback()
if err != nil {
log.Println("Rollback transaction err:", err.Error())
} else {
rtn = true
}
return rtn
}