Skip to content

Commit ba1ed36

Browse files
committed
Added some more examples
1 parent 51a9644 commit ba1ed36

6 files changed

+422
-51
lines changed

example_query_db_test.go

+30-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
11
package gorethink
22

3+
import (
4+
"fmt"
5+
)
6+
7+
// Create a database named ’superheroes’.
38
func ExampleDBCreate() {
4-
// Setup database
5-
DBCreate("test").Run(session)
9+
resp, err := DBCreate("superheroes").RunWrite(session)
10+
if err != nil {
11+
fmt.Print(err)
12+
}
13+
14+
fmt.Printf("%d DB created", resp.DBsCreated)
15+
// Output:
16+
// 1 DB created
17+
}
18+
19+
// Drop a database named ‘superheroes’.
20+
func ExampleDBDrop() {
21+
// Setup database + tables
22+
DBCreate("superheroes").Exec(session)
23+
DB("superheroes").TableCreate("superheroes").Exec(session)
24+
DB("superheroes").TableCreate("battles").Exec(session)
25+
26+
resp, err := DBDrop("superheroes").RunWrite(session)
27+
if err != nil {
28+
fmt.Print(err)
29+
}
30+
31+
fmt.Printf("%d DB dropped, %d tables dropped", resp.DBsDropped, resp.TablesDropped)
32+
// Output:
33+
// 1 DB dropped, 2 tables dropped
634
}

example_query_select_test.go

+94-47
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,126 @@ import (
44
"fmt"
55
)
66

7+
// Find a document by ID.
78
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
1327
}
28+
fmt.Print(hero["name"])
1429

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+
}
1932

33+
// Find a document and merge another document with it.
34+
func ExampleTerm_Get_Merge() {
2035
// 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)
2239
if err != nil {
23-
log.Fatalf("Error finding person: %s", err)
40+
fmt.Print(err)
41+
return
2442
}
43+
defer res.Close()
2544

2645
if res.IsNil() {
27-
log.Fatalf("Person not found")
46+
fmt.Print("Row not found")
47+
return
2848
}
2949

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)
3352
if err != nil {
34-
log.Fatalf("Error scanning database result: %s", err)
53+
fmt.Print("Error scanning database result: %s", err)
54+
return
3555
}
36-
fmt.Printf("%s %s (%s)", person.FirstName, person.LastName, person.Gender)
56+
fmt.Printf("%s: %v", hero["name"], hero["powers"])
3757

38-
// Output:
39-
// John Smith (M)
58+
// Output: The Flash: [speed]
4059
}
4160

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
4870
}
71+
defer res.Close()
4972

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+
}
5884

85+
// Get all users who are more than 25 years old.
86+
func ExampleTerm_Filter_Row() {
5987
// 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)
6189
if err != nil {
62-
log.Fatalf("Error finding person: %s", err)
90+
fmt.Print(err)
91+
return
6392
}
93+
defer res.Close()
6494

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
67101
}
102+
fmt.Printf("%d users", len(users))
68103

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
76116
}
117+
defer res.Close()
77118

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))
79127

80-
// Output:
81-
// John Smith (M)
128+
// Output: 1 users
82129
}

example_query_table_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
)
66

7+
// Create a table named "table" with the default settings.
78
func ExampleTerm_TableCreate() {
89
// Setup database
910
DB("test").TableDrop("table").Run(session)
@@ -19,6 +20,7 @@ func ExampleTerm_TableCreate() {
1920
// 1 table created
2021
}
2122

23+
// Create a simple index based on the field name.
2224
func ExampleTerm_IndexCreate() {
2325
// Setup database
2426
DB("test").TableDrop("table").Run(session)
@@ -35,6 +37,7 @@ func ExampleTerm_IndexCreate() {
3537
// 1 index created
3638
}
3739

40+
// Create a compound index based on the fields first_name and last_name.
3841
func ExampleTerm_IndexCreate_compound() {
3942
// Setup database
4043
DB("test").TableDrop("table").Run(session)

0 commit comments

Comments
 (0)