-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
122 lines (101 loc) · 3.22 KB
/
example_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
package hydrate_test
import (
"context"
"fmt"
"github.com/jinzhu/gorm"
"github.com/coursehero/hydrate"
)
//A single query can be provided using hydrate.NewQuery. Add model structs using AddModel providing a pointer to an
//instance of the model type and the alias used for the model in the query.
//The query provided should not include SELECT and should start with FROM.
func ExampleQuery() {
var db *gorm.DB
//example structs
type Author struct {
AuthorID uint `gorm:"primary_key"`
Name string
}
type Exercise struct {
ExerciseID uint `gorm:"primary_key"`
SectionID uint
Name string
}
type Section struct {
SectionID *uint `gorm:"primary_key"`
TextbookID uint
Title string
Exercises []Exercise `gorm:"foreignkey:SectionID;association_foreignkey:SectionID"`
}
type Textbook struct {
TextbookID uint `gorm:"primary_key"`
AuthorID uint
Name string
Author Author `gorm:"foreignkey:AuthorID;association_foreignkey:AuthorID"`
Sections []*Section `gorm:"foreignkey:TextbookID;association_foreignkey:TextbookID"`
}
var textbooks []Textbook
err := hydrate.NewQuery(db, `FROM textbooks t
LEFT JOIN sections s on t.textbook_id = s.textbook_id
LEFT JOIN exercises e ON e.section_id = s.section_id
LEFT JOIN authors a ON a.author_id = t.author_id
WHERE t.textbook_id in (?)
ORDER BY t.textbook_id, s.section_id, a.author_id`, 1).
AddModel(Textbook{}, "t").
AddModel(Section{}, "s").
AddModel(Exercise{}, "e").
AddModel(Author{}, "a").
Run(context.Background(), &textbooks)
if err != nil {
fmt.Printf("err = %v\n", err)
}
//all textbooks are fully loaded with all relationships
fmt.Printf("%d textbooks loaded\n", len(textbooks))
for _, t := range textbooks {
fmt.Printf("Textbook %d has %d sections\n", t.TextbookID, len(t.Sections))
for _, s := range t.Sections {
fmt.Printf("Section %d has %d exercises\n", s.SectionID, len(s.Exercises))
}
}
}
func ExampleMultiQuery() {
var db *gorm.DB
//example tables
type Author struct {
AuthorID uint `gorm:"primary_key"`
Name string
}
type Section struct {
SectionID *uint `gorm:"primary_key"`
TextbookID uint
Title string
}
type Textbook struct {
TextbookID uint `gorm:"primary_key"`
AuthorID uint
Name string
Author Author `gorm:"foreignkey:AuthorID;association_foreignkey:AuthorID"`
Sections []*Section `gorm:"foreignkey:TextbookID;association_foreignkey:TextbookID"`
}
var textbooks []Textbook
err := hydrate.MultiQuery{
hydrate.NewQuery(db, `FROM textbooks t
LEFT JOIN sections s on t.textbook_id = s.textbook_id
WHERE t.textbook_id in (?)
ORDER BY t.textbook_id, s.section_id`, 1).
AddModel(Textbook{}, "t").
AddModel(Section{}, "s"),
hydrate.NewQuery(db, `FROM textbooks t
LEFT JOIN authors a ON a.author_id = t.author_id
WHERE t.textbook_id in (?)
ORDER BY t.textbook_id, s.section_id, a.author_id`, 1).
AddModel(Author{}, "a"),
}.Run(context.Background(), &textbooks)
if err != nil {
fmt.Printf("err = %v\n", err)
}
//all textbooks are fully loaded with all relationships
fmt.Printf("%d textbooks loaded\n", len(textbooks))
for _, t := range textbooks {
fmt.Printf("Textbook %d has %d sections\n", t.TextbookID, len(t.Sections))
}
}