/
tx.go
100 lines (91 loc) · 1.94 KB
/
tx.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
package mysql
import (
"database/sql"
"reflect"
"time"
)
// Tx transaction.
type Tx struct {
db *conn
tx *sql.Tx
}
type conntx struct {
*sql.Tx
}
func (tx *Tx) Rollback() (err error) {
sqlLogger.Info("ROLLBACK")
return tx.tx.Rollback()
}
func (tx *Tx) Commit() (err error) {
sqlLogger.Info("COMMIT")
return tx.tx.Commit()
}
//Exec sql 更新或者插入使用
func (tx *Tx) Exec(sql string, params ...interface{}) (ret sql.Result, err error) {
a := time.Now()
ret, err = tx.tx.Exec(sql, params...)
debugLogQueies(sql, a, err, params...)
return
}
//Query extend sql.DB
func (tx *Tx) Query(sql string, params ...interface{}) (rows *sql.Rows, err error) {
a := time.Now()
rows, err = tx.tx.Query(sql, params...)
debugLogQueies(sql, a, err, params...)
return
}
//Insert insert
func (tx *Tx) Insert(obj interface{}) (id int64, err error) {
t := reflect.TypeOf(obj)
v := reflect.ValueOf(obj)
t = t.Elem()
v = v.Elem()
tableName := getTableName(t, v)
query, params := insert(t, v, tableName)
ret, err := tx.Exec(query, params...)
if err != nil {
return
}
id, err = ret.LastInsertId()
return
}
//Insert insert
func (tx *Tx) Update(obj interface{}) (id int64, err error) {
t := reflect.TypeOf(obj)
v := reflect.ValueOf(obj)
t = t.Elem()
v = v.Elem()
tableName := getTableName(t, v)
id, query, params := update(t, v, tableName)
_, err = tx.Exec(query, params...)
if err != nil {
return
}
return
}
// InsertOrUpdate InsertOrUpdate
func (tx *Tx) InsertOrUpdate(obj interface{}) (id int64, err error) {
t := reflect.TypeOf(obj)
v := reflect.ValueOf(obj)
t = t.Elem()
v = v.Elem()
tableName := getTableName(t, v)
_, pk := getPk(t, v)
var query string
var params []interface{}
if pk == 0 {
query, params = insert(t, v, tableName)
} else {
id, query, params = update(t, v, tableName)
}
ret, err := tx.Exec(query, params...)
if err != nil {
return
}
if id != 0 {
return
} else {
id, err = ret.LastInsertId()
}
return
}