-
Notifications
You must be signed in to change notification settings - Fork 19
/
example_select_test.go
306 lines (282 loc) · 7.96 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package genmai_test
import (
"fmt"
"log"
"github.com/naoina/genmai"
)
type M2 struct {
Id int64
Body string
}
func ExampleDB_Select_all() {
db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
if err != nil {
log.Fatal(err)
}
var results []TestModel
// SELECT "test_model".* FROM "test_model";
if err := db.Select(&results); err != nil {
log.Fatal(err)
}
fmt.Println(results)
}
func ExampleDB_Select_where() {
db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
if err != nil {
log.Fatal(err)
}
var results []TestModel
// SELECT "test_model".* FROM "test_model" WHERE "id" = 1;
if err := db.Select(&results, db.Where("id", "=", 1)); err != nil {
log.Fatal(err)
}
fmt.Println(results)
}
func ExampleDB_Select_whereAnd() {
db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
if err != nil {
log.Fatal(err)
}
var results []TestModel
// SELECT "test_model".* FROM "test_model" WHERE "id" = 1 AND "name" = "alice";
if err := db.Select(&results, db.Where("id", "=", 1).And("name", "=", "alice")); err != nil {
log.Fatal(err)
}
fmt.Println(results)
}
func ExampleDB_Select_whereNested() {
db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
if err != nil {
log.Fatal(err)
}
var results []TestModel
// SELECT "test_model".* FROM "test_model" WHERE "id" = 1 OR ("name" = "alice" AND "addr" != "Tokyo");
if err := db.Select(&results, db.Where("id", "=", 1).Or(db.Where("name", "=", "alice").And("addr", "!=", "Tokyo"))); err != nil {
log.Fatal(err)
}
fmt.Println(results)
}
func ExampleDB_Select_in() {
db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
if err != nil {
log.Fatal(err)
}
var results []TestModel
// SELECT "test_model".* FROM "test_model" WHERE "id" IN (1, 3, 5);
if err := db.Select(&results, db.Where("id").In(1, 3, 5)); err != nil {
log.Fatal(err)
}
fmt.Println(results)
}
func ExampleDB_Select_inWithSlice() {
db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
if err != nil {
log.Fatal(err)
}
var results []TestModel
values := []int64{1, 3, 5}
// SELECT "test_model".* FROM "test_model" WHERE "id" IN (1, 3, 5);
if err := db.Select(&results, db.Where("id").In(values)); err != nil {
log.Fatal(err)
}
fmt.Println(results)
}
func ExampleDB_Select_like() {
db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
if err != nil {
log.Fatal(err)
}
var results []TestModel
// SELECT "test_model".* FROM "test_model" WHERE "name" LIKE "alice%";
if err := db.Select(&results, db.Where("name").Like("alice%")); err != nil {
log.Fatal(err)
}
fmt.Println(results)
}
func ExampleDB_Select_between() {
db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
if err != nil {
log.Fatal(err)
}
var results []TestModel
// SELECT "test_model".* FROM "test_model" WHERE "id" BETWEEN 3 AND 5;
if err := db.Select(&results, db.Where("id").Between(3, 5)); err != nil {
log.Fatal(err)
}
fmt.Println(results)
}
func ExampleDB_Select_orderBy() {
db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
if err != nil {
log.Fatal(err)
}
var results []TestModel
// SELECT "test_model".* FROM "test_model" ORDER BY "name" DESC;
if err := db.Select(&results, db.OrderBy("name", genmai.DESC)); err != nil {
log.Fatal(err)
}
fmt.Println(results)
}
func ExampleDB_Select_orderByWithSpecificTable() {
db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
if err != nil {
log.Fatal(err)
}
var results []TestModel
// SELECT "test_model".* FROM "test_model" ORDER BY "test_model"."name" DESC;
if err := db.Select(&results, db.OrderBy(TestModel{}, "name", genmai.DESC)); err != nil {
log.Fatal(err)
}
fmt.Println(results)
}
func ExampleDB_Select_orderByMultiple() {
db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
if err != nil {
log.Fatal(err)
}
var results []TestModel
// SELECT "test_model".* FROM "test_model" ORDER BY "name" DESC, "addr" DESC;
if err := db.Select(&results, db.OrderBy("name", genmai.DESC, "addr", genmai.DESC)); err != nil {
log.Fatal(err)
}
fmt.Println(results)
}
func ExampleDB_Select_limit() {
db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
if err != nil {
log.Fatal(err)
}
var results []TestModel
// SELECT "test_model".* FROM "test_model" LIMIT 3;
if err := db.Select(&results, db.Limit(3)); err != nil {
log.Fatal(err)
}
fmt.Println(results)
}
func ExampleDB_Select_offset() {
db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
if err != nil {
log.Fatal(err)
}
var results []TestModel
// SELECT "test_model".* FROM "test_model" OFFSET 10;
if err := db.Select(&results, db.Offset(10)); err != nil {
log.Fatal(err)
}
fmt.Println(results)
}
func ExampleDB_Select_distinct() {
db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
if err != nil {
log.Fatal(err)
}
var results []TestModel
// SELECT DISTINCT "test_model"."name" FROM "test_model";
if err := db.Select(&results, db.Distinct("name")); err != nil {
log.Fatal(err)
}
fmt.Println(results)
}
func ExampleDB_Select_count() {
db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
if err != nil {
log.Fatal(err)
}
var result int64
// SELECT COUNT(*) FROM "test_model";
if err := db.Select(&result, db.Count(), db.From(TestModel{})); err != nil {
log.Fatal(err)
}
fmt.Println(result)
}
func ExampleDB_Select_countDistinct() {
db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
if err != nil {
log.Fatal(err)
}
var result int64
// SELECT COUNT(DISTINCT "test_model"."name") FROM "test_model";
if err := db.Select(&result, db.Count(db.Distinct("name")), db.From(TestModel{})); err != nil {
log.Fatal(err)
}
fmt.Println(result)
}
func ExampleDB_Select_columns() {
db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
if err != nil {
log.Fatal(err)
}
var results []TestModel
// SELECT "test_model"."id", "test_model"."name" FROM "test_model";
if err := db.Select(&results, []string{"id", "name"}); err != nil {
log.Fatal(err)
}
fmt.Println(results)
}
func ExampleDB_Select_complex() {
db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
if err != nil {
log.Fatal(err)
}
var results []TestModel
// SELECT "test_model"."name" FROM "test_model"
// WHERE "name" LIKE "%alice%" OR ("id" > 100 AND "id" < 200) OR ("id" BETWEEN 700 AND 1000)
// ORDER BY "id" ASC LIMIT 2 OFFSET 5
if err := db.Select(&results, "name", db.Where("name").
Like("%alice%").
Or(db.Where("id", ">", 100).And("id", "<", 200)).
Or(db.Where("id").Between(700, 1000)).
Limit(2).Offset(5).OrderBy("id", genmai.ASC),
); err != nil {
log.Fatal(err)
}
fmt.Println(results)
}
func ExampleDB_Select_join() {
db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
if err != nil {
log.Fatal(err)
}
type M2 struct {
Id int64
Body string
}
var results []TestModel
// SELECT "test_model".* FROM "test_model" JOIN "m2" ON "test_model"."id" = "m2"."id";
if err := db.Select(&results, "name", db.Join(&M2{}).On("id")); err != nil {
log.Fatal(err)
}
fmt.Println(results)
}
func ExampleDB_Select_joinWithSpecificTable() {
db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
if err != nil {
log.Fatal(err)
}
type M2 struct {
Id int64
Body string
}
type M3 struct {
TestModelId int64
M2Id int64
}
var results []TestModel
// SELECT "test_model".* FROM "test_model" JOIN "m3" ON "test_model"."id" = "m3"."test_model_id" JOIN "m2" ON "m3"."m2_id" = "m2"."id";
if err := db.Select(&results, "name", db.Join(&M3{}).On("id", "=", "test_model_id"), db.Join(&M2{}).On(&M3{}, "m2_id", "=", "id")); err != nil {
log.Fatal(err)
}
fmt.Println(results)
}
func ExampleDB_Select_leftJoin() {
db, err := genmai.New(&genmai.SQLite3Dialect{}, ":memory:")
if err != nil {
log.Fatal(err)
}
var results []TestModel
// SELECT "test_model".* FROM "test_model" LEFT JOIN "m2" ON "test_model"."name" = "m2"."body" WHERE "m2"."body" IS NULL;
if err := db.Select(&results, "name", db.LeftJoin(&M2{}).On("name", "=", "body").Where(&M2{}, "body").IsNull()); err != nil {
log.Fatal(err)
}
fmt.Println(results)
}