-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_select_test.go
174 lines (146 loc) · 4.45 KB
/
example_select_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package magicsql_test
import (
"fmt"
"github.com/Nerdmaster/magicsql"
_ "github.com/mattn/go-sqlite3"
)
func ExampleSelect_EachRow() {
var db, err = magicsql.Open("sqlite3", "./test.db")
if err != nil {
panic(err)
}
var op = db.Operation()
op.Exec("DROP TABLE IF EXISTS people; CREATE TABLE people (name TEXT, age INT)")
op.Exec("INSERT INTO people VALUES ('Joe', 100), ('Jill', 101), ('Doug', 102), ('Deb', 103)")
var person struct {
Name string
Age int
}
op.Select("people", &person).EachRow(func(r magicsql.Scannable) {
r.Scan(&person.Name, &person.Age)
fmt.Printf("%s is %d years old\n", person.Name, person.Age)
fmt.Printf("That's %d in dog years!\n", person.Age*7)
})
// Output:
// Joe is 100 years old
// That's 700 in dog years!
// Jill is 101 years old
// That's 707 in dog years!
// Doug is 102 years old
// That's 714 in dog years!
// Deb is 103 years old
// That's 721 in dog years!
}
func ExampleSelect_EachObject() {
var db, err = magicsql.Open("sqlite3", "./test.db")
if err != nil {
panic(err)
}
var op = db.Operation()
op.Exec("DROP TABLE IF EXISTS people; CREATE TABLE people (name TEXT, age INT)")
op.Exec("INSERT INTO people VALUES ('Joe', 100), ('Jill', 101), ('Doug', 102), ('Deb', 103)")
var person struct {
Name string
Age int
}
// The "&person" duplication is a necessary evil - we have to tell the Select
// shortcut what structure to magick up, and then we have to tell EachObject
// where to put its data
op.Select("people", &person).EachObject(&person, func() {
fmt.Printf("%s is %d dog years old\n", person.Name, person.Age*7)
fmt.Printf("That's %d in people years!\n", person.Age)
})
// This makes more sense when using an OperationTable rather than a
// one-off select. For instance:
var t = op.Table("people", &person)
person.Name = "Pat"
person.Age = 1
t.Insert(&person)
t.Select().Where("Age < ?", 100).EachObject(&person, func() {
fmt.Printf("%s is only %d dog years old\n", person.Name, person.Age*7)
})
// Output:
// Joe is 700 dog years old
// That's 100 in people years!
// Jill is 707 dog years old
// That's 101 in people years!
// Doug is 714 dog years old
// That's 102 in people years!
// Deb is 721 dog years old
// That's 103 in people years!
// Pat is only 7 dog years old
}
// This shows pulling a single record from the database without having to deal
// with any looping
func ExampleSelect_First() {
var db, err = magicsql.Open("sqlite3", "./test.db")
if err != nil {
panic(err)
}
var op = db.Operation()
op.Exec("DROP TABLE IF EXISTS people; CREATE TABLE people (name TEXT, age INT)")
op.Exec("INSERT INTO people VALUES ('Joe', 0), ('Jill', 1), ('Doug', 2), ('Deb', 3)")
var person struct {
Name string
Age int
}
var ok = op.Select("people", &person).Order("age desc").First(&person)
fmt.Printf("%#v, %#v\n", ok, person)
// Output:
// true, struct { Name string; Age int }{Name:"Deb", Age:3}
}
// This shows Select.First when there's no data to pull
func ExampleSelect_First_noData() {
var db, err = magicsql.Open("sqlite3", "./test.db")
if err != nil {
panic(err)
}
var op = db.Operation()
op.Exec("DROP TABLE IF EXISTS people; CREATE TABLE people (name TEXT, age INT)")
var person struct {
Name string
Age int
}
person.Name = "Not initialized"
var ok = op.Select("people", &person).Order("age desc").First(&person)
fmt.Printf("%#v, %#v\n", ok, person)
// Output:
// false, struct { Name string; Age int }{Name:"Not initialized", Age:0}
}
// This is an example of using Select.Query to work with raw rows
func ExampleSelect_Query() {
var db, err = magicsql.Open("sqlite3", "./test.db")
if err != nil {
panic(err)
}
var op = db.Operation()
op.Exec("DROP TABLE IF EXISTS people; CREATE TABLE people (name TEXT, age INT)")
op.Exec("INSERT INTO people VALUES ('Joe', 0), ('Jill', 1), ('Doug', 2), ('Deb', 3)")
var person struct {
Name string
Age int
}
var r = op.Select("people", &person).Query()
for r.Next() {
r.Scan(&person.Name, &person.Age)
fmt.Printf("%s is %d years old\n", person.Name, person.Age)
// Let's use a more scientific computation for dog years here
var dy = person.Age * 4
if person.Age >= 1 {
dy += 8
}
if person.Age >= 2 {
dy += 8
}
fmt.Printf("That's %d in dog years!\n", dy)
}
// Output:
// Joe is 0 years old
// That's 0 in dog years!
// Jill is 1 years old
// That's 12 in dog years!
// Doug is 2 years old
// That's 24 in dog years!
// Deb is 3 years old
// That's 28 in dog years!
}