Debian Codegraph @

internal/integration/tests/example_query_control_test.go

raw symbols SCIP
1 package tests
2
3 import (
4 "fmt"
5 r "gopkg.in/rethinkdb/rethinkdb-go.v6"
6 )
7
8 // Return heroes and superheroes.
9 func ExampleBranch() {
10 cur, err := r.DB("examples").Table("marvel").OrderBy("name").Map(r.Branch(
11 r.Row.Field("victories").Gt(100),
12 r.Row.Field("name").Add(" is a superhero"),
13 r.Row.Field("name").Add(" is a hero"),
14 )).Run(session)
15 if err != nil {
16 fmt.Print(err)
17 return
18 }
19
20 var strs []string
21 err = cur.All(&strs)
22 if err != nil {
23 fmt.Print(err)
24 return
25 }
26
27 for _, str := range strs {
28 fmt.Println(str)
29 }
30
31 // Output:
32 // Iron Man is a superhero
33 // Jubilee is a hero
34 }
35
36 // Return an error
37 func ExampleError() {
38 err := r.Error("this is a runtime error").Exec(session)
39 fmt.Println(err)
40 }
41
42 // Suppose we want to retrieve the titles and authors of the table posts. In the
43 // case where the author field is missing or null, we want to retrieve the
44 // string "Anonymous".
45 func ExampleTerm_Default() {
46 cur, err := r.DB("examples").Table("posts").Map(map[string]interface{}{
47 "title": r.Row.Field("title"),
48 "author": r.Row.Field("author").Default("Anonymous"),
49 }).Run(session)
50 if err != nil {
51 fmt.Print(err)
52 return
53 }
54
55 var res map[string]interface{}
56 err = cur.One(&res)
57 if err != nil {
58 fmt.Print(err)
59 return
60 }
61
62 fmt.Print(res)
63 }
64
65 // Convert a Go integer to a ReQL object
66 func ExampleExpr_int() {
67 cur, err := r.Expr(1).Run(session)
68 if err != nil {
69 fmt.Print(err)
70 return
71 }
72
73 var res interface{}
74 err = cur.One(&res)
75 if err != nil {
76 fmt.Print(err)
77 return
78 }
79
80 jsonPrint(res)
81
82 // Output: 1
83 }
84
85 // Convert a Go slice to a ReQL object
86 func ExampleExpr_slice() {
87 cur, err := r.Expr([]int{1, 2, 3}).Run(session)
88 if err != nil {
89 fmt.Print(err)
90 return
91 }
92
93 var res []interface{}
94 err = cur.All(&res)
95 if err != nil {
96 fmt.Print(err)
97 return
98 }
99
100 jsonPrint(res)
101
102 // Output:
103 // [
104 // 1,
105 // 2,
106 // 3
107 // ]
108 }
109
110 // Convert a Go slice to a ReQL object
111 func ExampleExpr_map() {
112 cur, err := r.Expr(map[string]interface{}{
113 "a": 1,
114 "b": "b",
115 }).Run(session)
116 if err != nil {
117 fmt.Print(err)
118 return
119 }
120
121 var res interface{}
122 err = cur.One(&res)
123 if err != nil {
124 fmt.Print(err)
125 return
126 }
127
128 jsonPrint(res)
129
130 // Output:
131 // {
132 // "a": 1,
133 // "b": "b"
134 // }
135 }
136
137 // Convert a Go slice to a ReQL object
138 func ExampleExpr_struct() {
139 type ExampleTypeNested struct {
140 N int
141 }
142
143 type ExampleTypeEmbed struct {
144 C string
145 }
146
147 type ExampleTypeA struct {
148 ExampleTypeEmbed
149
150 A int
151 B string
152 Nested ExampleTypeNested
153 }
154
155 cur, err := r.Expr(ExampleTypeA{
156 A: 1,
157 B: "b",
158 ExampleTypeEmbed: ExampleTypeEmbed{
159 C: "c",
160 },
161 Nested: ExampleTypeNested{
162 N: 2,
163 },
164 }).Run(session)
165 if err != nil {
166 fmt.Print(err)
167 return
168 }
169
170 var res interface{}
171 err = cur.One(&res)
172 if err != nil {
173 fmt.Print(err)
174 return
175 }
176
177 jsonPrint(res)
178
179 // Output:
180 // {
181 // "A": 1,
182 // "B": "b",
183 // "C": "c",
184 // "Nested": {
185 // "N": 2
186 // }
187 // }
188 }
189
190 // Convert a Go struct (with rethinkdb tags) to a ReQL object. The tags allow
191 // the field names to be changed.
192 func ExampleExpr_structTags() {
193 type ExampleType struct {
194 A int `rethinkdb:"field_a"`
195 B string `rethinkdb:"field_b"`
196 }
197
198 cur, err := r.Expr(ExampleType{
199 A: 1,
200 B: "b",
201 }).Run(session)
202 if err != nil {
203 fmt.Print(err)
204 return
205 }
206
207 var res interface{}
208 err = cur.One(&res)
209 if err != nil {
210 fmt.Print(err)
211 return
212 }
213
214 jsonPrint(res)
215
216 // Output:
217 // {
218 // "field_a": 1,
219 // "field_b": "b"
220 // }
221 }
222
223 // Execute a raw JSON query
224 func ExampleRawQuery() {
225 cur, err := r.RawQuery([]byte(`"hello world"`)).Run(session)
226 if err != nil {
227 fmt.Print(err)
228 return
229 }
230
231 var res interface{}
232 err = cur.One(&res)
233 if err != nil {
234 fmt.Print(err)
235 return
236 }
237
238 jsonPrint(res)
239
240 // Output: "hello world"
241 }