forked from gohouse/gorose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_test.go
176 lines (152 loc) · 3.42 KB
/
session_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package gorose
import (
"errors"
"testing"
)
func initSession() ISession {
return initDB().NewSession()
}
func TestSession_Query(t *testing.T) {
var s = initSession()
//var user []Users
res, err := s.Query("select * from users where name=?", "gorose2")
if err != nil {
t.Error(err.Error())
}
//t.Log(res)
t.Log(res, s.LastSql())
}
func TestSession_Query3(t *testing.T) {
var s = initSession()
var o []Users
//var o []map[string]interface{}
//var o []gorose.Data
res, err := s.Bind(&o).Query("select * from users limit 2")
if err != nil {
t.Error(err.Error())
}
t.Log(res)
t.Log(o, s.LastSql())
}
func TestSession_Execute(t *testing.T) {
var sql = `CREATE TABLE IF NOT EXISTS "orders" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"goodsname" TEXT NOT NULL default "",
"price" decimal default "0.00"
)`
var s = initSession()
var err error
var aff int64
aff, err = s.Execute(sql)
if err != nil {
t.Error(err.Error())
}
if aff == 0 {
return
}
aff, err = s.Execute("insert into orders(goodsname,price) VALUES(?,?),(?,?)",
"goods1", 1.23, "goods2", 3.23)
if err != nil {
t.Error(err.Error())
}
t.Log(aff)
}
func TestSession_Query_struct(t *testing.T) {
var s = initSession()
var err error
// defer s.Close()
var user []Users
_, err = s.Bind(&user).Query("select * from users limit ?", 2)
if err != nil {
t.Error(err.Error())
}
t.Log("多条struct绑定:", user)
var user2 Users
_, err = s.Bind(&user2).Query("select * from users limit ?", 2)
if err != nil {
t.Error(err.Error())
}
t.Log("一条struct绑定:", user2)
}
//type UserMap map[string]interface{}
func TestSession_Query_map(t *testing.T) {
var s = initSession()
var err error
var user2 = aaa{}
_, err = s.Bind(&user2).Query("select * from users limit ?", 2)
if err != nil {
t.Error(err.Error())
}
t.Log("一条map绑定:", user2)
t.Log("一条map绑定的uid为:", user2["uid"])
t.Log(s.LastSql())
var user = bbb{}
_, err = s.Bind(&user).Query("select * from users limit ?", 2)
if err != nil {
t.Error(err.Error())
}
t.Log("多条map绑定:", user)
t.Log("多条map绑定:", user[0]["age"].Int())
t.Log(s.LastSql())
}
func TestSession_Bind(t *testing.T) {
var s = initSession()
var err error
var user2 = aaa{}
_, err = s.Bind(&user2).Query("select * from users limit ?", 2)
if err != nil {
t.Error(err.Error())
}
t.Log("session bind success")
}
func TestSession_Transaction(t *testing.T) {
var s = initSession()
// 一键事务, 自动回滚和提交, 我们只需要关注业务即可
err := s.Transaction(trans1, trans2)
if err != nil {
t.Error(err.Error())
}
t.Log("session transaction success")
}
func trans1(s ISession) error {
var err error
var aff int64
aff, err = s.Execute("update users set name=?,age=? where uid=?",
"gorose3", 21, 3)
if err != nil {
return err
}
if aff == 0 {
return errors.New("fail")
}
aff, err = s.Execute("update users set name=?,age=? where uid=?",
"gorose2", 20, 2)
if err != nil {
return err
}
if aff == 0 {
return errors.New("fail")
}
return nil
}
func trans2(s ISession) error {
var err error
var aff int64
aff, err = s.Execute("update users set name=?,age=? where uid=?",
"gorose3", 21, 3)
if err != nil {
return err
}
if aff == 0 {
return errors.New("fail")
}
aff, err = s.Execute("update users set name=?,age=? where uid=?",
"gorose2", 20, 2)
if err != nil {
return err
}
if aff == 0 {
return errors.New("fail")
}
return nil
}