forked from gohouse/gorose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
orm_query_test.go
358 lines (306 loc) · 7.18 KB
/
orm_query_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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package gorose
import (
"errors"
"fmt"
"testing"
"time"
)
func TestOrm_BuildSql2(t *testing.T) {
db := DB()
var u = "age=age+1,num=num+1"
var wheres interface{}
wheres = [][]interface{}{{"a", ">", "b"}, {"a", "b"}, {"a is null"}}
sqlstr, a, b := db.Force().Table("users").Data(u).Where(wheres).BuildSql("update")
t.Log(sqlstr, a, b)
}
func TestOrm_BuildSql3(t *testing.T) {
db := DB()
var u = "age=age+1,num=num+1"
var wheres interface{}
wheres = [][]interface{}{{"a", ">", "b"}, {"a", "b"}}
sqlstr, a, b := db.Force().Table(Users{}).Data(u).Where(wheres).BuildSql("update")
t.Log(sqlstr, a, b)
}
func TestOrm_BuildSql4(t *testing.T) {
db := DB()
//var wheres interface{}
//wheres = [][]interface{}{{"a", ">", "b"},{"lock",1}}
wheres := Data{"lock": 1,"`date`": 1}
obj := db.Table(Users{}).Where(wheres).Where(func() {
db.Where("c", 2).OrWhere("lock", ">", 4)
}).Data(wheres)
sqlstr, a, b := obj.BuildSql()
t.Log(sqlstr, a, b)
sqlstr, a, b = obj.BuildSql("update")
t.Log(sqlstr, a, b)
sqlstr, a, b = obj.BuildSql("insert")
t.Log(sqlstr, a, b)
}
func TestOrm_BuildSql5(t *testing.T) {
go func(t *testing.T) {
for {
//<-ticker.C
db := DB()
sqlstr, a, b := db.Table("users").Where("uid", ">", 1).BuildSql()
//c,d := db.Table("users").Get()
//t.Log(db.LastSql())
count, d := db.First()
t.Log(sqlstr, a, b)
t.Log(count, d)
t.Log(db.LastSql())
}
}(t)
time.Sleep(500 * time.Millisecond)
}
func TestOrm_BuildSql6(t *testing.T) {
var db = DB()
sqlstr, a, b := db.Table("users3").Limit(2).Offset(2).BuildSql()
t.Log(sqlstr, a, b)
sqlstr, a, b = db.Table("users2").Limit(2).Offset(2).BuildSql()
t.Log(sqlstr, a, b)
var u = Users{
Uid: 1111,
Name: "2",
Age: 3,
}
res, err := db.Table("xxx").Where("xx", "xx").Update(&u)
t.Log(db.LastSql(), res, err)
}
func TestOrm_First(t *testing.T) {
res, err := DB().Table(Users{}).Where("uid", 1).First()
if err != nil {
t.Error(err.Error())
}
t.Log(res)
}
func TestOrm_Select(t *testing.T) {
db := DB()
var err error
var u = []Users{}
err = db.Table(&u).Select()
t.Log(err, u, db.LastSql())
var u2 = Users{}
err = db.Table(&u2).Select()
t.Log(err, u2, db.LastSql())
var u3 Users
err = db.Table(&u3).Select()
t.Log(err, u3, db.LastSql())
var u4 []Users
err = db.Table(&u4).Limit(2).Select()
t.Log(err, u4, db.LastSql())
if err != nil {
t.Error(err.Error())
}
t.Log(u, u2, u3, u4)
}
func TestOrm_Select2(t *testing.T) {
db := DB()
var err error
var u = []UsersMap{}
err = db.Table(&u).Limit(2).Select()
if err != nil {
t.Error(err.Error())
}
t.Log(u)
var u3 = UsersMap{}
err = db.Table(&u3).Limit(1).Select()
if err != nil {
t.Error(err.Error())
}
t.Log(u)
}
type Users2 struct {
Name string `orm:"name"`
Age int `orm:"age"`
Uid int `orm:"uid"`
Fi string `orm:"ignore"`
}
func (u *Users2) TableName() string {
return "users"
}
func TestOrm_Get2(t *testing.T) {
db := DB()
var err error
var u []Users2
res, err := db.Table("users").Where("uid", ">", 2).
Where("1","=","1").
//Where("1 = 1").
Limit(2).Get()
//res, err := db.Table(&u).Where("uid", ">", 0).Limit(2).Get()
fmt.Println(db.LastSql())
if err != nil {
t.Error(err.Error())
}
t.Log(res, u)
}
func TestOrm_Get(t *testing.T) {
orm := DB()
var u = UsersMap{}
ormObj := orm.Table(&u).Join("b", "a.id", "=", "b.id").
RightJoin("userinfo d on a.id=d.id").
Fields("a.uid,a.age").
Order("uid desc").
Where("a", 1).
WhereNull("bb").
WhereNotNull("cc").
WhereIn("dd", []interface{}{1, 2}).
OrWhereNotIn("ee", []interface{}{1, 2}).
WhereBetween("ff", []interface{}{11, 21}).
WhereNotBetween("ff", []interface{}{1, 2}).
Where("a", "like", "%3%").
OrWhere(func() {
orm.Where("c", 3).OrWhere(func() {
orm.Where("d", ">", 4)
})
}).Where("e", 5).Limit(5).Offset(2)
s, a, err := ormObj.BuildSql()
if err != nil {
t.Error(err.Error())
}
t.Log(s, a, u)
}
func TestOrm_Pluck(t *testing.T) {
orm := DB()
//var u = UsersMapSlice{}
//var u []Users
ormObj := orm.Table("users")
//res,err := ormObj.Pluck("name", "uid")
res, err := ormObj.Limit(5).Pluck("name", "uid")
if err != nil {
t.Error(err.Error())
}
t.Log(res, orm.LastSql())
}
func TestOrm_Value(t *testing.T) {
db := DB()
//var u = UsersMap{}
//var u = UsersMapSlice{}
//var u Users
//var u []Users
//ormObj := db.Table(&u)
//ormObj := db.Table("users")
ormObj := db.Table(Users{})
res, err := ormObj.Limit(5).Value("uid")
if err != nil {
t.Error(err.Error())
}
t.Log(res, db.LastSql())
}
func TestOrm_Count(t *testing.T) {
db := DB()
//var u = UsersMap{}
//ormObj := db.Table(&u)
ormObj := db.Table("users")
res, err := ormObj.Count()
if err != nil {
t.Error(err.Error())
}
t.Log(res, db.LastSql())
}
func TestOrm_Count2(t *testing.T) {
var u Users
var count int64
count, err := DB().Table(&u).Count()
if err != nil {
t.Error(err.Error())
}
t.Log(count)
}
func TestOrm_Chunk(t *testing.T) {
orm := DB()
var u = []UsersMap{}
err := orm.Table(&u).Chunk(1, func(data []Data) error {
for _, item := range data {
t.Log(item["name"])
}
return errors.New("故意停止,防止数据过多,浪费时间")
//return nil
})
if err != nil && err.Error() != "故意停止,防止数据过多,浪费时间" {
t.Error(err.Error())
}
t.Log("Chunk() success")
}
func TestOrm_Chunk2(t *testing.T) {
orm := DB()
var u []Users
var i int
err := orm.Table(&u).ChunkStruct(2, func() error {
//for _, item := range u {
t.Log(u)
//}
if i == 2 {
return errors.New("故意停止,防止数据过多,浪费时间")
}
i++
return nil
})
if err != nil && err.Error() != "故意停止,防止数据过多,浪费时间" {
t.Error(err.Error())
}
t.Log("ChunkStruct() success")
}
func TestOrm_Loop(t *testing.T) {
db := DB()
var u = []UsersMap{}
//aff,err := db.Table(&u).Force().Data(Data{"age": 18}).Update()
//fmt.Println(aff,err)
err := db.Table(&u).Where("age", 18).Loop(2, func(data []Data) error {
for _, item := range data {
_, err := DB().Table(&u).Data(Data{"age": 19}).Where("uid", item["uid"]).Update()
if err != nil {
t.Error(err.Error())
}
}
return errors.New("故意停止,防止数据过多,浪费时间")
//return nil
})
if err != nil && err.Error() != "故意停止,防止数据过多,浪费时间" {
t.Error(err.Error())
}
t.Log("Loop() success")
}
func TestOrm_Paginate(t *testing.T) {
db := DB()
var u []Users
res, err := db.Table(&u).Limit(2).Paginate()
if err != nil {
t.Error(err.Error())
}
t.Log(res, u)
t.Log(db.LastSql())
}
func TestOrm_Paginate2(t *testing.T) {
db := DB()
var u []Users
res, err := db.Table(&u).Where("uid", ">", 1).Limit(2).Paginate(3)
if err != nil {
t.Error(err.Error())
}
t.Log(res, u)
t.Log(db.LastSql())
}
func TestOrm_Sum(t *testing.T) {
db := DB()
var u Users
//res, err := db.Table(Users{}).First()
res, err := db.Table(&u).Where(Data{"uid": 1}).Sum("age")
if err != nil {
t.Error(err.Error())
}
//fmt.Printf("%#v\n",res)
t.Log(res, u)
t.Log(db.LastSql())
}
func BenchmarkNewOrm(b *testing.B) {
engin := initDB()
for i := 0; i < b.N; i++ {
engin.NewOrm().Table("users").First()
}
}
func BenchmarkNewOrm2(b *testing.B) {
engin := initDB()
for i := 0; i < b.N; i++ {
engin.NewOrm().Table("users").First()
}
}