This repository has been archived by the owner on Jun 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
testJoin.go
142 lines (122 loc) · 3.08 KB
/
testJoin.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
package tests
import (
"fmt"
"testing"
"time"
"github.com/go-xorm/xorm"
)
func joinSameMapper(engine *xorm.Engine, t *testing.T) {
var users []Userinfo
err := engine.Join("LEFT", "`Userdetail`", "`Userinfo`.`(id)`=`Userdetail`.`Id`").Find(&users)
if err != nil {
t.Error(err)
panic(err)
}
}
func join(engine *xorm.Engine, t *testing.T) {
var users []Userinfo
err := engine.Join("LEFT", "userdetail", "userinfo.id=userdetail.id").Find(&users)
if err != nil {
t.Error(err)
panic(err)
}
join2(engine, t)
join3(engine, t)
joinCond(engine, t)
joinCount(engine, t)
joinCount2(engine, t)
}
func joinCond(engine *xorm.Engine, t *testing.T) {
var users []Userinfo
err := engine.Join("LEFT", "userdetail", "userinfo.id=userdetail.id AND userinfo.id > ?", 0).Find(&users,
&Userinfo{Uid: 1})
if err != nil {
t.Error(err)
panic(err)
}
var user Userinfo
_, err = engine.Join("LEFT", "userdetail", "userinfo.id=userdetail.id AND userinfo.id > ?", 0).Id(1).Get(&user)
if err != nil {
t.Error(err)
panic(err)
}
total, err := engine.Join("LEFT", "userdetail", "userinfo.id=userdetail.id AND userinfo.id > ?", 0).Where("userinfo.id = ?", 1).Count(&Userinfo{})
if err != nil {
t.Error(err)
panic(err)
}
fmt.Println(total)
err = engine.Join("LEFT", "userdetail", "userinfo.id=userdetail.id AND userinfo.id > ?", 0).Iterate(&Userinfo{Uid: 1}, func(i int, bean interface{}) error {
fmt.Println(i)
return nil
})
if err != nil {
t.Error(err)
panic(err)
}
rows, err := engine.Join("LEFT", "userdetail", "userinfo.id=userdetail.id AND userinfo.id > ?", 0).Rows(&Userinfo{Uid: 1})
if err != nil {
t.Error(err)
panic(err)
}
var user2 Userinfo
for rows.Next() {
err = rows.Scan(&user2)
if err != nil {
t.Error(err)
panic(err)
}
}
}
func join2(engine *xorm.Engine, t *testing.T) {
var users []Userinfo
err := engine.Join("LEFT", "userdetail", "userinfo.id=userdetail.id").Find(&users,
&Userinfo{Uid: 1})
if err != nil {
t.Error(err)
panic(err)
}
}
func join3(engine *xorm.Engine, t *testing.T) {
_, err := engine.Join("LEFT", "userdetail", "userinfo.id=userdetail.id").Get(&Userinfo{Uid: 1})
if err != nil {
t.Error(err)
panic(err)
}
}
func joinCount(engine *xorm.Engine, t *testing.T) {
count, err := engine.Join("LEFT", "userdetail", "userinfo.id=userdetail.id").Count(&Userinfo{Uid: 1})
if err != nil {
t.Error(err)
panic(err)
}
fmt.Println(count)
}
type History struct {
Rid int64
Uid int64
DeletedAt time.Time `xorm:"deleted"`
}
type Resource struct {
Rid int64
}
type NewUser struct {
Uid int64
}
func (NewUser) TableName() string {
return "user"
}
func joinCount2(engine *xorm.Engine, t *testing.T) {
err := engine.Sync2(new(History), new(Resource), new(NewUser))
if err != nil {
t.Error(err)
panic(err)
}
var where = "`history`.`deleted_at` > '0001-01-01 00:00:00'"
count, err := engine.Table("history").Join("LEFT", "resource", "`resource`.`rid`=`history`.`rid`").Join("LEFT", "user", "`user`.`uid`=`history`.`uid`").Where(where).Count(new(History))
if err != nil {
t.Error(err)
panic(err)
}
fmt.Println(count)
}