@@ -4,79 +4,126 @@ import (
4
4
"fmt"
5
5
)
6
6
7
+ // Find a document by ID.
7
8
func ExampleTerm_Get () {
8
- type Person struct {
9
- ID string `gorethink:"id, omitempty"`
10
- FirstName string `gorethink:"first_name"`
11
- LastName string `gorethink:"last_name"`
12
- Gender string `gorethink:"gender"`
9
+ // Fetch the row from the database
10
+ res , err := DB ("test" ).Table ("heroes" ).Get (2 ).Run (session )
11
+ if err != nil {
12
+ fmt .Print (err )
13
+ return
14
+ }
15
+ defer res .Close ()
16
+
17
+ if res .IsNil () {
18
+ fmt .Print ("Row not found" )
19
+ return
20
+ }
21
+
22
+ var hero map [string ]interface {}
23
+ err = res .One (& hero )
24
+ if err != nil {
25
+ fmt .Print ("Error scanning database result: %s" , err )
26
+ return
13
27
}
28
+ fmt .Print (hero ["name" ])
14
29
15
- // Setup table
16
- DB ("test" ).TableDrop ("table" ).Run (session )
17
- DB ("test" ).TableCreate ("table" ).Run (session )
18
- DB ("test" ).Table ("table" ).Insert (Person {"1" , "John" , "Smith" , "M" }).Run (session )
30
+ // Output: Superman
31
+ }
19
32
33
+ // Find a document and merge another document with it.
34
+ func ExampleTerm_Get_Merge () {
20
35
// Fetch the row from the database
21
- res , err := DB ("test" ).Table ("table" ).Get ("1" ).Run (session )
36
+ res , err := DB ("test" ).Table ("heroes" ).Get (4 ).Merge (map [string ]interface {}{
37
+ "powers" : []string {"speed" },
38
+ }).Run (session )
22
39
if err != nil {
23
- log .Fatalf ("Error finding person: %s" , err )
40
+ fmt .Print (err )
41
+ return
24
42
}
43
+ defer res .Close ()
25
44
26
45
if res .IsNil () {
27
- log .Fatalf ("Person not found" )
46
+ fmt .Print ("Row not found" )
47
+ return
28
48
}
29
49
30
- // Scan query result into the person variable
31
- var person Person
32
- err = res .One (& person )
50
+ var hero map [string ]interface {}
51
+ err = res .One (& hero )
33
52
if err != nil {
34
- log .Fatalf ("Error scanning database result: %s" , err )
53
+ fmt .Print ("Error scanning database result: %s" , err )
54
+ return
35
55
}
36
- fmt .Printf ("%s %s (%s) " , person . FirstName , person . LastName , person . Gender )
56
+ fmt .Printf ("%s: %v " , hero [ "name" ], hero [ "powers" ] )
37
57
38
- // Output:
39
- // John Smith (M)
58
+ // Output: The Flash: [speed]
40
59
}
41
60
42
- func ExampleTerm_GetAll_compound () {
43
- type Person struct {
44
- ID string `gorethink:"id, omitempty"`
45
- FirstName string `gorethink:"first_name"`
46
- LastName string `gorethink:"last_name"`
47
- Gender string `gorethink:"gender"`
61
+ // Get all users who are 30 years old.
62
+ func ExampleTerm_Filter () {
63
+ // Fetch the row from the database
64
+ res , err := DB ("test" ).Table ("users" ).Filter (map [string ]interface {}{
65
+ "age" : 30 ,
66
+ }).Run (session )
67
+ if err != nil {
68
+ fmt .Print (err )
69
+ return
48
70
}
71
+ defer res .Close ()
49
72
50
- // Setup table
51
- DB ("test" ).TableDrop ("table" ).Run (session )
52
- DB ("test" ).TableCreate ("table" ).Run (session )
53
- DB ("test" ).Table ("table" ).Insert (Person {"1" , "John" , "Smith" , "M" }).Run (session )
54
- DB ("test" ).Table ("table" ).IndexCreateFunc ("full_name" , func (row Term ) interface {} {
55
- return []interface {}{row .Field ("first_name" ), row .Field ("last_name" )}
56
- }).Run (session )
57
- DB ("test" ).Table ("table" ).IndexWait ().Run (session )
73
+ // Scan query result into the person variable
74
+ var users []interface {}
75
+ err = res .All (& users )
76
+ if err != nil {
77
+ fmt .Print ("Error scanning database result: %s" , err )
78
+ return
79
+ }
80
+ fmt .Printf ("%d users" , len (users ))
81
+
82
+ // Output: 2 users
83
+ }
58
84
85
+ // Get all users who are more than 25 years old.
86
+ func ExampleTerm_Filter_Row () {
59
87
// Fetch the row from the database
60
- res , err := DB ("test" ).Table ("table " ).GetAllByIndex ( "full_name" , [] interface {}{ "John" , "Smith" } ).Run (session )
88
+ res , err := DB ("test" ).Table ("users " ).Filter ( Row . Field ( "age" ). Gt ( 25 ) ).Run (session )
61
89
if err != nil {
62
- log .Fatalf ("Error finding person: %s" , err )
90
+ fmt .Print (err )
91
+ return
63
92
}
93
+ defer res .Close ()
64
94
65
- if res .IsNil () {
66
- log .Fatalf ("Person not found" )
95
+ // Scan query result into the person variable
96
+ var users []interface {}
97
+ err = res .All (& users )
98
+ if err != nil {
99
+ fmt .Print ("Error scanning database result: %s" , err )
100
+ return
67
101
}
102
+ fmt .Printf ("%d users" , len (users ))
68
103
69
- // Scan query result into the person variable
70
- var person Person
71
- err = res .One (& person )
72
- if err == ErrEmptyResult {
73
- log .Fatalf ("Person not found" )
74
- } else if err != nil {
75
- log .Fatalf ("Error scanning database result: %s" , err )
104
+ // Output: 3 users
105
+ }
106
+
107
+ // Retrieve all users who have a gmail account (whose field email ends with @gmail.com).
108
+ func ExampleTerm_Filter_Function () {
109
+ // Fetch the row from the database
110
+ res , err := DB ("test" ).Table ("users" ).Filter (func (user Term ) Term {
111
+ return user .Field ("email" ).Match ("@gmail.com$" )
112
+ }).Run (session )
113
+ if err != nil {
114
+ fmt .Print (err )
115
+ return
76
116
}
117
+ defer res .Close ()
77
118
78
- fmt .Printf ("%s %s (%s)" , person .FirstName , person .LastName , person .Gender )
119
+ // Scan query result into the person variable
120
+ var users []interface {}
121
+ err = res .All (& users )
122
+ if err != nil {
123
+ fmt .Print ("Error scanning database result: %s" , err )
124
+ return
125
+ }
126
+ fmt .Printf ("%d users" , len (users ))
79
127
80
- // Output:
81
- // John Smith (M)
128
+ // Output: 1 users
82
129
}
0 commit comments