-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
121 lines (101 loc) · 2.98 KB
/
main.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
package main
import (
"fmt"
"../dbschema"
"github.com/admpub/null"
_ "github.com/go-sql-driver/mysql"
"github.com/webx-top/db"
"github.com/webx-top/db/_tools/test/settings"
"github.com/webx-top/db/lib/sqlbuilder"
"github.com/webx-top/echo"
)
type GroupAndVHosts struct {
*dbschema.NgingVhostGroup
Vhosts []*dbschema.NgingVhost `db:"-,relation=group_id:id"` //relation=<vhost的字段>:<vhost_group的字段>
}
type GroupAndVHost struct {
*dbschema.NgingVhostGroup
Vhost *dbschema.NgingVhost `db:"-,relation=group_id:id"` //relation=<vhost的字段>:<vhost_group的字段>
}
type JoinData struct {
Group *dbschema.NgingVhostGroup
Vhost *dbschema.NgingVhost
}
type FilmWithCategory struct {
*dbschema.OfficialFilmItem
TypeList []*dbschema.OfficialFilmType `db:",relation=id:types|split"`
}
func testMultiOne2many2(c db.Database) {
rows2 := []*FilmWithCategory{}
err := c.Collection(`official_film_item`).Find().All(&rows2)
if err != nil {
panic(err)
}
echo.Dump(rows2)
}
func testJoin(c db.Database) {
jrow := &JoinData{}
c.Collection(`nging_vhost_group`).Find().Callback(func(sel sqlbuilder.Selector) sqlbuilder.Selector {
return sel.From(`nging_vhost_group g`).Join(`nging_vhost v`).On(`v.group_id=g.id`)
}).One(jrow)
echo.Dump(jrow)
}
func testOne2one(c db.Database) {
row := &GroupAndVHost{}
err := c.(sqlbuilder.Database).SelectFrom(`nging_vhost_group`).Relation(`Vhost`, func(sel sqlbuilder.Selector) sqlbuilder.Selector {
return sel.OrderBy(`-id`)
}).One(row)
if err != nil {
panic(err)
}
echo.Dump(row)
}
func testMultiOne2one(c db.Database) {
rows := []*GroupAndVHost{}
//Relation 是可选的,用于增加额外条件
err := c.(sqlbuilder.Database).SelectFrom(`nging_vhost_group`).Relation(`Vhost`, func(sel sqlbuilder.Selector) sqlbuilder.Selector {
return sel.OrderBy(`-id`)
}).All(&rows)
if err != nil {
panic(err)
}
echo.Dump(rows)
}
func testMultiOne2many(c db.Database) {
rows2 := []*GroupAndVHosts{}
err := c.Collection(`nging_vhost_group`).Find().Relation(`Vhosts`, func(sel sqlbuilder.Selector) sqlbuilder.Selector {
return sel.OrderBy(`id`) //.ForceIndex(`group_id`)
}).All(&rows2)
if err != nil {
panic(err)
}
echo.Dump(rows2)
}
func testMap(c db.Database) {
row2 := null.StringMap{}
err := c.(sqlbuilder.Database).SelectFrom(`nging_vhost_group`).One(&row2)
if err != nil {
panic(err)
}
echo.Dump(row2)
rows3 := null.StringMapSlice{}
err = c.(sqlbuilder.Database).SelectFrom(`nging_vhost_group`).Limit(2).All(&rows3)
if err != nil {
panic(err)
}
echo.Dump(rows3)
}
func main() {
c := settings.Connect()
testMultiOne2many2(c)
//return
testJoin(c)
//return
testOne2one(c)
fmt.Println(`===========================================`)
testMultiOne2one(c)
fmt.Println(`===========================================`)
testMultiOne2many(c)
//验证map方式是否正常==============================
testMap(c)
}