-
Notifications
You must be signed in to change notification settings - Fork 9
/
orm_execute_test.go
101 lines (89 loc) · 1.85 KB
/
orm_execute_test.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
101
package gorose
import (
"fmt"
"testing"
)
func TestOrm_Update(t *testing.T) {
db := DB()
var u = []Users{{
Name: "gorose2",
Age: 19,
}}
aff, err := db.Force().Update(&u)
if err != nil {
t.Error(err.Error())
}
t.Log(aff, db.LastSql())
}
func TestOrm_Update2(t *testing.T) {
db := DB()
//var u = []Users{{
// Name: "gorose2",
// Age: 11,
//}}
aff, err := db.Table("users").Where("uid", 1).Update()
if err != nil {
//t.Error(err.Error())
t.Log(err.Error())
return
}
t.Log(aff, db.LastSql())
}
func TestOrm_UpdateMap(t *testing.T) {
db := DB()
//var u = []UsersMap{{"name": "gorose2", "age": 19}}
var u = UsersMap{"name": "gorose2", "age": 19}
aff, err := db.Force().Update(&u)
if err != nil {
t.Error(err.Error())
}
t.Log(aff, db.LastSql())
}
func TestTrans(t *testing.T) {
var db = DB()
var db2 = DB()
var res Users
db.Begin()
db2.Table(&res).Select()
t.Log(res)
db.Commit()
t.Log(res)
}
func Test_Transaction(t *testing.T) {
var db = DB()
// 一键事务, 自动回滚和提交, 我们只需要关注业务即可
err := db.Transaction(
func(db IOrm) error {
//db.Table("users").Limit(2).SharedLock().Get()
//fmt.Println(db.LastSql())
_, err := db.Table("users").Where("uid", 2).Update(Data{"name": "gorose2"})
fmt.Println(db.LastSql())
if err != nil {
return err
}
_, err = db.Insert(&UsersMap{"name": "gorose2", "age": 0})
fmt.Println(db.LastSql())
if err != nil {
return err
}
return nil
},
func(db IOrm) error {
_, err := db.Table("users").Where("uid", 3).Update(Data{"name": "gorose3"})
fmt.Println(db.LastSql())
if err != nil {
return err
}
_, err = db.Insert(&UsersMap{"name": "gorose2", "age": 0})
fmt.Println(db.LastSql())
if err != nil {
return err
}
return nil
},
)
if err != nil {
t.Error(err.Error())
}
t.Log("事务测试通过")
}