-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrow_test.go
88 lines (77 loc) · 1.68 KB
/
row_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
package freedb
import (
"context"
"fmt"
"github.com/FreeLeh/GoFreeDB/google/auth"
)
func ExampleGoogleSheetRowStore() {
// Initialize authentication
googleAuth, err := auth.NewServiceFromFile(
"<path_to_service_account_file>",
GoogleAuthScopes,
auth.ServiceConfig{},
)
if err != nil {
panic(err)
}
// Create row store with columns definition
store := NewGoogleSheetRowStore(
googleAuth,
"<spreadsheet_id>",
"<sheet_name>",
GoogleSheetRowStoreConfig{
Columns: []string{"name", "age", "email"},
},
)
// Insert some rows
type Person struct {
Name string `db:"name"`
Age int `db:"age"`
Email string `db:"email"`
}
err = store.Insert(
Person{Name: "Alice", Age: 30, Email: "[email protected]"},
Person{Name: "Bob", Age: 25, Email: "[email protected]"},
).Exec(context.Background())
if err != nil {
panic(err)
}
// Query rows
var people []Person
err = store.Select(&people).
Where("age > ?", 20).
OrderBy([]ColumnOrderBy{{Column: "age", OrderBy: OrderByAsc}}).
Limit(10).
Exec(context.Background())
if err != nil {
panic(err)
}
fmt.Println("Selected people:", people)
// Update rows
update := map[string]interface{}{"age": 31}
err = store.Update(update).Where("name = ?", "Alice").
Exec(context.Background())
if err != nil {
panic(err)
}
// Count rows
count, err := store.Count().
Where("age > ?", 20).
Exec(context.Background())
if err != nil {
panic(err)
}
fmt.Println("Number of people over 20:", count)
// Delete rows
err = store.Delete().
Where("name = ?", "Bob").
Exec(context.Background())
if err != nil {
panic(err)
}
// Clean up
err = store.Close(context.Background())
if err != nil {
panic(err)
}
}