Debian Codegraph @

internal/integration/tests/example_query_select_test.go

raw symbols SCIP
1 package tests
2
3 import (
4 "fmt"
5 r "gopkg.in/rethinkdb/rethinkdb-go.v6"
6 )
7
8 // Find a document by ID.
9 func ExampleTerm_Get() {
10 // Fetch the row from the database
11 res, err := r.DB("examples").Table("heroes").Get(2).Run(session)
12 if err != nil {
13 fmt.Print(err)
14 return
15 }
16 defer res.Close()
17
18 if res.IsNil() {
19 fmt.Print("Row not found")
20 return
21 }
22
23 var hero map[string]interface{}
24 err = res.One(&hero)
25 if err != nil {
26 fmt.Printf("Error scanning database result: %s", err)
27 return
28 }
29 fmt.Print(hero["name"])
30
31 // Output: Superman
32 }
33
34 // Find a document by ID.
35 func ExampleTerm_GetAll() {
36 // Fetch the row from the database
37 res, err := r.DB("examples").Table("heroes").GetAll(2).Run(session)
38 if err != nil {
39 fmt.Print(err)
40 return
41 }
42 defer res.Close()
43
44 if res.IsNil() {
45 fmt.Print("Row not found")
46 return
47 }
48
49 var hero map[string]interface{}
50 err = res.One(&hero)
51 if err != nil {
52 fmt.Printf("Error scanning database result: %s", err)
53 return
54 }
55 fmt.Print(hero["name"])
56
57 // Output: Superman
58 }
59
60 // Find a document by ID.
61 func ExampleTerm_GetAll_multiple() {
62 // Fetch the row from the database
63 res, err := r.DB("examples").Table("heroes").GetAll(1, 2).Run(session)
64 if err != nil {
65 fmt.Print(err)
66 return
67 }
68 defer res.Close()
69
70 var heroes []map[string]interface{}
71 err = res.All(&heroes)
72 if err != nil {
73 fmt.Printf("Error scanning database result: %s", err)
74 return
75 }
76 fmt.Print(heroes[0]["name"])
77
78 // Output: Superman
79 }
80
81 // Find all document with an indexed value.
82 func ExampleTerm_GetAll_optArgs() {
83 // Fetch the row from the database
84 res, err := r.DB("examples").Table("heroes").GetAll("man_of_steel").OptArgs(r.GetAllOpts{
85 Index: "code_name",
86 }).Run(session)
87 if err != nil {
88 fmt.Print(err)
89 return
90 }
91 defer res.Close()
92
93 if res.IsNil() {
94 fmt.Print("Row not found")
95 return
96 }
97
98 var hero map[string]interface{}
99 err = res.One(&hero)
100 if err != nil {
101 fmt.Printf("Error scanning database result: %s", err)
102 return
103 }
104 fmt.Print(hero["name"])
105
106 // Output: Superman
107 }
108
109 // Find all document with an indexed value.
110 func ExampleTerm_GetAllByIndex() {
111 // Fetch the row from the database
112 res, err := r.DB("examples").Table("heroes").GetAllByIndex("code_name", "man_of_steel").Run(session)
113 if err != nil {
114 fmt.Print(err)
115 return
116 }
117 defer res.Close()
118
119 if res.IsNil() {
120 fmt.Print("Row not found")
121 return
122 }
123
124 var hero map[string]interface{}
125 err = res.One(&hero)
126 if err != nil {
127 fmt.Printf("Error scanning database result: %s", err)
128 return
129 }
130 fmt.Print(hero["name"])
131
132 // Output: Superman
133 }
134
135 // Find a document and merge another document with it.
136 func ExampleTerm_Get_merge() {
137 // Fetch the row from the database
138 res, err := r.DB("examples").Table("heroes").Get(4).Merge(map[string]interface{}{
139 "powers": []string{"speed"},
140 }).Run(session)
141 if err != nil {
142 fmt.Print(err)
143 return
144 }
145 defer res.Close()
146
147 if res.IsNil() {
148 fmt.Print("Row not found")
149 return
150 }
151
152 var hero map[string]interface{}
153 err = res.One(&hero)
154 if err != nil {
155 fmt.Printf("Error scanning database result: %s", err)
156 return
157 }
158 fmt.Printf("%s: %v", hero["name"], hero["powers"])
159
160 // Output: The Flash: [speed]
161 }
162
163 // Get all users who are 30 years old.
164 func ExampleTerm_Filter() {
165 // Fetch the row from the database
166 res, err := r.DB("examples").Table("users").Filter(map[string]interface{}{
167 "age": 30,
168 }).Run(session)
169 if err != nil {
170 fmt.Print(err)
171 return
172 }
173 defer res.Close()
174
175 // Scan query result into the person variable
176 var users []interface{}
177 err = res.All(&users)
178 if err != nil {
179 fmt.Printf("Error scanning database result: %s", err)
180 return
181 }
182 fmt.Printf("%d users", len(users))
183
184 // Output: 2 users
185 }
186
187 // Get all users who are more than 25 years old.
188 func ExampleTerm_Filter_row() {
189 // Fetch the row from the database
190 res, err := r.DB("examples").Table("users").Filter(r.Row.Field("age").Gt(25)).Run(session)
191 if err != nil {
192 fmt.Print(err)
193 return
194 }
195 defer res.Close()
196
197 // Scan query result into the person variable
198 var users []interface{}
199 err = res.All(&users)
200 if err != nil {
201 fmt.Printf("Error scanning database result: %s", err)
202 return
203 }
204 fmt.Printf("%d users", len(users))
205
206 // Output: 3 users
207 }
208
209 // Retrieve all users who have a gmail account (whose field email ends with @gmail.com).
210 func ExampleTerm_Filter_function() {
211 // Fetch the row from the database
212 res, err := r.DB("examples").Table("users").Filter(func(user r.Term) r.Term {
213 return user.Field("email").Match("@gmail.com$")
214 }).Run(session)
215 if err != nil {
216 fmt.Print(err)
217 return
218 }
219 defer res.Close()
220
221 // Scan query result into the person variable
222 var users []interface{}
223 err = res.All(&users)
224 if err != nil {
225 fmt.Printf("Error scanning database result: %s", err)
226 return
227 }
228 fmt.Printf("%d users", len(users))
229
230 // Output: 1 users
231 }