-
Notifications
You must be signed in to change notification settings - Fork 12
/
example_test.go
116 lines (105 loc) · 1.96 KB
/
example_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package sqlittle_test
import (
"fmt"
"github.com/alicebob/sqlittle"
)
// Basic SELECT
func ExampleDB_Select() {
db, err := sqlittle.Open("./testdata/music.sqlite")
if err != nil {
panic(err)
}
defer db.Close()
db.Select(
"tracks",
func(r sqlittle.Row) {
var (
name string
length int
)
_ = r.Scan(&name, &length)
fmt.Printf("%s: %d seconds\n", name, length)
},
"name",
"length",
)
// output:
// Drive My Car: 145 seconds
// Norwegian Wood: 121 seconds
// You Wont See Me: 198 seconds
// Come Together: 259 seconds
// Something: 182 seconds
// Maxwells Silver Hammer: 207 seconds
}
// SELECT in index order
func ExampleDB_IndexedSelect() {
db, err := sqlittle.Open("./testdata/music.sqlite")
if err != nil {
panic(err)
}
defer db.Close()
db.IndexedSelect(
"tracks",
"tracks_length",
func(r sqlittle.Row) {
var (
name string
length int
)
_ = r.Scan(&name, &length)
fmt.Printf("%s: %d seconds\n", name, length)
},
"name",
"length",
)
// output:
// Norwegian Wood: 121 seconds
// Drive My Car: 145 seconds
// Something: 182 seconds
// You Wont See Me: 198 seconds
// Maxwells Silver Hammer: 207 seconds
// Come Together: 259 seconds
}
func ExampleDB_IndexedSelectEq() {
db, err := sqlittle.Open("./testdata/music.sqlite")
if err != nil {
panic(err)
}
defer db.Close()
db.IndexedSelectEq(
"tracks",
"tracks_length",
sqlittle.Key{198},
func(r sqlittle.Row) {
var (
name string
length int
)
_ = r.Scan(&name, &length)
fmt.Printf("%s: %d seconds\n", name, length)
},
"name",
"length",
)
// output:
// You Wont See Me: 198 seconds
}
// SELECT a primary key
func ExampleDB_PKSelect() {
db, err := sqlittle.Open("./testdata/music.sqlite")
if err != nil {
panic(err)
}
defer db.Close()
db.PKSelect(
"tracks",
sqlittle.Key{4},
func(r sqlittle.Row) {
name, _ := r.ScanString()
fmt.Printf("%s\n", name)
},
"name",
)
// output:
// Come Together
}