-
Notifications
You must be signed in to change notification settings - Fork 1
/
last.go
67 lines (63 loc) · 1.58 KB
/
last.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
package CoreSQL
import (
"database/sql"
"errors"
"github.com/jmoiron/sqlx"
)
//LastRows 事务关系尾部封装
func LastRows(tx *sqlx.Tx, err error) error {
if err != nil {
err2 := tx.Rollback()
if err2 != nil {
return errors.New("rollback failed: " + err.Error() + ", err: " + err2.Error())
}
return err
}
return nil
}
//LastRowsAffected 事务关系尾封装,带影响行判断
func LastRowsAffected(tx *sqlx.Tx, result sql.Result, err error) error {
if err != nil {
err2 := tx.Rollback()
if err2 != nil {
return errors.New("rollback failed: " + err.Error() + ", err: " + err2.Error())
}
return err
}else{
var rowsAffected int64
rowsAffected, err = result.RowsAffected()
if err == nil{
if rowsAffected < 1{
err2 := tx.Rollback()
if err2 != nil {
return errors.New("rollback failed: " + err2.Error())
}
return errors.New("no rows affected")
}
}
}
return nil
}
//LastRowsAffectedCreate 带有ID的创建尾部
func LastRowsAffectedCreate(tx *sqlx.Tx, stmt *sqlx.NamedStmt, args interface{}, err error) (int64, error) {
if err != nil {
err2 := tx.Rollback()
if err2 != nil {
return 0, errors.New("rollback failed: " + err.Error() + ", err: " + err2.Error())
}
return 0, errors.New("prepare named " + err.Error())
}
defer func() {
_ = stmt.Close()
}()
var lastID int64
err = stmt.Get(&lastID, args)
if err != nil {
err2 := tx.Rollback()
if err2 != nil {
return 0, errors.New("rollback failed: " + err.Error() + ", err: " + err2.Error())
}
return 0, errors.New("get last id " + err.Error())
}
return lastID, nil
}