-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite_db_retrieve.go
575 lines (513 loc) · 17.1 KB
/
sqlite_db_retrieve.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
package buna
import (
"context"
"database/sql"
"fmt"
"reflect"
)
func (s *SQLiteDB) getBrewingMethodsByLastAdded(ctx context.Context, limit int) ([]brewingMethod, error) {
brewingMethods := make([]brewingMethod, 0, limit)
if err := s.TransactContext(ctx, func(ctx context.Context, tx *sql.Tx) error {
rows, err := tx.QueryContext(ctx, `
SELECT name
FROM brewing_methods
ORDER BY id DESC
LIMIT :limit
`,
sql.Named("limit", limit),
)
if err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to retrieve brewing method rows: %w", err)
}
defer rows.Close()
for rows.Next() {
var brewingMethod brewingMethod
if err := rows.Scan(&brewingMethod.name); err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to scan row: %w", err)
}
brewingMethods = append(brewingMethods, brewingMethod)
}
if err := rows.Err(); err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to scan last row: %w", err)
}
return nil
}); err != nil {
return nil, fmt.Errorf("buna: sqlite_db_retrieve: getBrewingMethodsLastAdded transaction failed: %w", err)
}
return brewingMethods, nil
}
func (s *SQLiteDB) getBrewingsOrderByDesc(ctx context.Context, limit int, orderByName string) ([]brewing, error) {
brewings := make([]brewing, 0, limit)
if err := s.TransactContext(ctx, func(ctx context.Context, tx *sql.Tx) error {
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT b.date,
c.name,
c.roaster,
m.name,
b.roast_date,
g.name,
b.grind_setting,
b.total_brewing_time_sec,
b.coffee_grams,
b.water_grams,
b.v60_filter_type,
b.rating,
b.recommended_grind_setting_adjustment,
b.recommended_coffee_weight_adjustment_grams,
b.notes
FROM brewings AS b
INNER JOIN coffees AS c
ON c.id = b.coffee_id
INNER JOIN brewing_methods AS m
ON m.id = b.method_id
INNER JOIN grinders AS g
ON g.id = b.grinder_id
ORDER BY b.%s DESC, b.id DESC
LIMIT :limit
`, orderByName),
sql.Named("limit", limit),
)
if err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to retrieve brewing rows: %w", err)
}
defer rows.Close()
for rows.Next() {
var brewing brewing
var roastDate, v60FilterType, rating, recommendedGrindSettingAdjustment, recommendedCoffeeWeightAdjustmentGrams, notes interface{}
if err := rows.Scan(
&brewing.date,
&brewing.coffeeName,
&brewing.coffeeRoaster,
&brewing.brewingMethodName,
&roastDate,
&brewing.grinderName,
&brewing.grindSetting,
&brewing.totalBrewingTimeSec,
&brewing.coffeeGrams,
&brewing.waterGrams,
&v60FilterType,
&rating,
&recommendedGrindSettingAdjustment,
&recommendedCoffeeWeightAdjustmentGrams,
¬es,
); err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to scan row: %w", err)
}
// Deal with possible NULL values
if v := reflect.ValueOf(roastDate); v.Kind() == reflect.String {
brewing.roastDate = roastDate.(string)
} else {
brewing.roastDate = "Unknown"
}
if v := reflect.ValueOf(v60FilterType); v.Kind() == reflect.String {
brewing.v60FilterType = v60FilterType.(string)
} else {
brewing.v60FilterType = "None"
}
if v := reflect.ValueOf(rating); v.Kind() == reflect.Int64 {
brewing.rating = int(rating.(int64))
} else {
brewing.rating = 0
}
if v := reflect.ValueOf(recommendedGrindSettingAdjustment); v.Kind() == reflect.String {
brewing.recommendedGrindSettingAdjustment = recommendedGrindSettingAdjustment.(string)
} else {
brewing.recommendedGrindSettingAdjustment = "None"
}
if v := reflect.ValueOf(recommendedCoffeeWeightAdjustmentGrams); v.Kind() == reflect.Float64 {
brewing.recommendedCoffeeWeightAdjustmentGrams = recommendedCoffeeWeightAdjustmentGrams.(float64)
} else {
brewing.recommendedCoffeeWeightAdjustmentGrams = 0
}
if v := reflect.ValueOf(notes); v.Kind() == reflect.String {
brewing.notes = notes.(string)
} else {
brewing.notes = "None"
}
brewings = append(brewings, brewing)
}
if err := rows.Err(); err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to scan last row: %w", err)
}
return nil
}); err != nil {
return nil, fmt.Errorf("buna: sqlite_db_retrieve: getBrewingsByLastAdded transaction failed: %w", err)
}
return brewings, nil
}
// The following fields are used from the brewingFilter argument:
// brewingMethodName, v60FilterType, coffeeName, coffeeRoaster, coffeeGrams, waterGrams, grinderName
func (s *SQLiteDB) getBrewingSuggestions(ctx context.Context, limit int, brewingFilter brewing) ([]brewing, error) {
brewings := make([]brewing, 0, limit)
if err := s.TransactContext(ctx, func(ctx context.Context, tx *sql.Tx) error {
rows, err := tx.QueryContext(ctx, `
SELECT b.grind_setting,
b.total_brewing_time_sec,
b.coffee_grams,
b.water_grams,
b.recommended_grind_setting_adjustment,
b.recommended_coffee_weight_adjustment_grams,
b.rating,
b.v60_filter_type,
c.name,
b.date,
g.name,
b.notes
FROM brewings AS b
INNER JOIN coffees AS c
ON c.id = b.coffee_id
INNER JOIN brewing_methods AS m
ON m.id = b.method_id
INNER JOIN grinders AS g
ON g.id = b.grinder_id
WHERE (m.name = :brewingMethodName)
AND (b.v60_filter_type = :v60FilterType OR "" = :v60FilterType)
AND (c.name = :coffeeName OR "" = :coffeeName)
AND (c.roaster = :coffeeRoaster OR "" = :coffeeRoaster)
AND (b.coffee_grams = :coffeeGrams OR 0 = :coffeeGrams)
AND (b.water_grams = :waterGrams OR 0 = :waterGrams)
AND (g.name = :grinderName OR "" = :grinderName)
ORDER BY b.id DESC
LIMIT :limit
`,
sql.Named("limit", limit),
sql.Named("brewingMethodName", brewingFilter.brewingMethodName),
sql.Named("v60FilterType", brewingFilter.v60FilterType),
sql.Named("coffeeName", brewingFilter.coffeeName),
sql.Named("coffeeRoaster", brewingFilter.coffeeRoaster),
sql.Named("coffeeGrams", brewingFilter.coffeeGrams),
sql.Named("waterGrams", brewingFilter.waterGrams),
sql.Named("grinderName", brewingFilter.grinderName),
)
if err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to retrieve brewing suggestion rows: %w", err)
}
defer rows.Close()
for rows.Next() {
var brewing brewing
var recommendedGrindSettingAdjustment, recommendedCoffeeWeightAdjustmentGrams, rating, v60FilterType, notes interface{}
if err := rows.Scan(
&brewing.grindSetting,
&brewing.totalBrewingTimeSec,
&brewing.coffeeGrams,
&brewing.waterGrams,
&recommendedGrindSettingAdjustment,
&recommendedCoffeeWeightAdjustmentGrams,
&rating,
&v60FilterType,
&brewing.coffeeName,
&brewing.date,
&brewing.grinderName,
¬es,
); err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to scan row: %w", err)
}
// Deal with possible NULL values
if v := reflect.ValueOf(recommendedGrindSettingAdjustment); v.Kind() == reflect.String {
brewing.recommendedGrindSettingAdjustment = recommendedGrindSettingAdjustment.(string)
} else {
brewing.recommendedGrindSettingAdjustment = "None"
}
if v := reflect.ValueOf(recommendedCoffeeWeightAdjustmentGrams); v.Kind() == reflect.Float64 {
brewing.recommendedCoffeeWeightAdjustmentGrams = recommendedCoffeeWeightAdjustmentGrams.(float64)
} else {
brewing.recommendedCoffeeWeightAdjustmentGrams = 0
}
if v := reflect.ValueOf(rating); v.Kind() == reflect.Int64 {
brewing.rating = int(rating.(int64))
} else {
brewing.rating = 0
}
if v := reflect.ValueOf(v60FilterType); v.Kind() == reflect.String {
brewing.v60FilterType = v60FilterType.(string)
} else {
brewing.v60FilterType = "None"
}
if v := reflect.ValueOf(notes); v.Kind() == reflect.String {
brewing.notes = notes.(string)
} else {
brewing.notes = "None"
}
brewings = append(brewings, brewing)
}
if err := rows.Err(); err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to scan last row: %w", err)
}
return nil
}); err != nil {
return nil, fmt.Errorf("buna: sqlite_db_retrieve: getBrewingSuggestions transaction failed: %w", err)
}
return brewings, nil
}
func (s *SQLiteDB) getCoffeeIDByNameRoaster(ctx context.Context, name string, roaster string) (int, error) {
var coffeeID int
if err := s.TransactContext(ctx, func(ctx context.Context, tx *sql.Tx) error {
if err := tx.QueryRowContext(ctx, `
SELECT id from coffees
WHERE name = :name AND roaster = :roaster
`,
sql.Named("name", name),
sql.Named("roaster", roaster),
).Scan(&coffeeID); err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to retrieve coffee id from db: %w", err)
}
return nil
}); err != nil {
return 0, fmt.Errorf("buna: sqlite_db_retrieve: getCoffeeIDByNameRoaster transaction failed: %w", err)
}
return coffeeID, nil
}
func (s *SQLiteDB) getCoffeePurchasesByLastAdded(ctx context.Context, limit int) ([]coffeePurchase, error) {
coffeePurchases := make([]coffeePurchase, 0, limit)
if err := s.TransactContext(ctx, func(ctx context.Context, tx *sql.Tx) error {
rows, err := tx.QueryContext(ctx, `
SELECT c.name, c.roaster, p.bought_date, p.roast_date
FROM purchases AS p
INNER JOIN coffees AS c
ON p.coffee_id = c.id
ORDER BY p.id DESC
LIMIT :limit
`,
sql.Named("limit", limit),
)
if err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to retrieve coffee purchase rows: %w", err)
}
defer rows.Close()
for rows.Next() {
var coffeePurchase coffeePurchase
var roastDate interface{}
if err := rows.Scan(&coffeePurchase.coffeeName, &coffeePurchase.coffeeRoaster, &coffeePurchase.boughtDate, &roastDate); err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to scan row: %w", err)
}
// Deal with possible NULL values
if v := reflect.ValueOf(roastDate); v.Kind() == reflect.String {
coffeePurchase.roastDate = roastDate.(string)
} else {
coffeePurchase.roastDate = "Unknown"
}
coffeePurchases = append(coffeePurchases, coffeePurchase)
}
if err := rows.Err(); err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to scan last row: %w", err)
}
return nil
}); err != nil {
return nil, fmt.Errorf("buna: sqlite_db_retrieve: getCoffeePurchasesByLastAdded transaction failed: %w", err)
}
return coffeePurchases, nil
}
func (s *SQLiteDB) getCoffeesByLastAdded(ctx context.Context, limit int) ([]coffee, error) {
coffees := make([]coffee, 0, limit)
if err := s.TransactContext(ctx, func(ctx context.Context, tx *sql.Tx) error {
rows, err := tx.QueryContext(ctx, `
SELECT name, roaster, region, variety, method, decaf
FROM coffees
ORDER BY id DESC
LIMIT :limit
`,
sql.Named("limit", limit),
)
if err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to retrieve coffee rows: %w", err)
}
defer rows.Close()
for rows.Next() {
var coffee coffee
var region, variety, method, decaf interface{}
if err := rows.Scan(&coffee.name, &coffee.roaster, ®ion, &variety, &method, &decaf); err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to scan row: %w", err)
}
// Deal with possible NULL values
if v := reflect.ValueOf(region); v.Kind() == reflect.String {
coffee.region = region.(string)
} else {
coffee.region = "Unknown"
}
if v := reflect.ValueOf(variety); v.Kind() == reflect.String {
coffee.variety = variety.(string)
} else {
coffee.variety = "Unknown"
}
if v := reflect.ValueOf(method); v.Kind() == reflect.String {
coffee.method = method.(string)
} else {
coffee.method = "Unknown"
}
if v := reflect.ValueOf(decaf); v.Kind() == reflect.Bool {
coffee.decaf = decaf.(bool)
}
coffees = append(coffees, coffee)
}
if err := rows.Err(); err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to scan last row: %w", err)
}
return nil
}); err != nil {
return nil, fmt.Errorf("buna: sqlite_db_retrieve: getCoffeesByLastAdded transaction failed: %w", err)
}
return coffees, nil
}
func (s *SQLiteDB) getCuppingsByLastAdded(ctx context.Context, limit int) ([]cupping, error) {
var cuppings []cupping
if err := s.TransactContext(ctx, func(ctx context.Context, tx *sql.Tx) error {
var realLimit interface{}
if err := tx.QueryRowContext(ctx, `
SELECT sum(coffeesPerCupping)
FROM (
SELECT cu.id, count(*) as coffeesPerCupping
FROM cuppings AS cu
INNER JOIN cupped_coffees AS cc
ON cu.id = cc.cupping_id
GROUP BY cu.id
LIMIT :limit
)
`,
sql.Named("limit", limit),
).Scan(&realLimit); err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to retrieve real cupping limit from db: %w", err)
}
var realLimitInt int64
if v := reflect.ValueOf(realLimit); v.Kind() == reflect.Int64 {
realLimitInt = realLimit.(int64)
} else {
// realLimit == NULL
return nil
}
rows, err := tx.QueryContext(ctx, `
SELECT cu.id, cu.date, cu.duration_min, cu.notes, c.name, cc.rank, cc.notes
FROM cuppings AS cu
INNER JOIN cupped_coffees AS cc
ON cu.id = cc.cupping_id
INNER JOIN coffees AS c
ON c.id = cc.coffee_id
ORDER BY cu.id DESC, cc.rank
LIMIT :limit
`,
sql.Named("limit", realLimitInt),
)
if err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to retrieve cupping rows: %w", err)
}
defer rows.Close()
isFirstRow := true
var prevCuppingID int
var cupping cupping
var cuppedCoffees []cuppedCoffee
for rows.Next() {
var cuppingID int
var coffee cuppedCoffee
if err := rows.Scan(
&cuppingID,
&cupping.date,
&cupping.durationMin,
&cupping.notes,
&coffee.name,
&coffee.rank,
&coffee.notes,
); err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to scan cupping row: %w", err)
}
if isFirstRow {
prevCuppingID = cuppingID
isFirstRow = false
}
if prevCuppingID != cuppingID {
cupping.cuppedCoffees = make([]cuppedCoffee, len(cuppedCoffees))
copy(cupping.cuppedCoffees, cuppedCoffees)
cuppings = append(cuppings, cupping)
cuppedCoffees = cuppedCoffees[:0]
prevCuppingID = cuppingID
}
cuppedCoffees = append(cuppedCoffees, coffee)
}
cupping.cuppedCoffees = cuppedCoffees
cuppings = append(cuppings, cupping)
if err := rows.Err(); err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to scan last row: %w", err)
}
return nil
}); err != nil {
return nil, fmt.Errorf("buna: sqlite_db_retrieve: getCuppingsByLastAdded transaction failed: %w", err)
}
return cuppings, nil
}
func (s *SQLiteDB) getGrinderIDByName(ctx context.Context, name string) (int, error) {
var grinderID int
if err := s.TransactContext(ctx, func(ctx context.Context, tx *sql.Tx) error {
if err := tx.QueryRowContext(ctx, `
SELECT id
FROM grinders
WHERE name = :grinderName
`,
sql.Named("grinderName", name),
).Scan(&grinderID); err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to retrieve grinder id from db: %w", err)
}
return nil
}); err != nil {
return 0, fmt.Errorf("buna: sqlite_db_retrieve: getGrinderIDByName transaction failed: %w", err)
}
return grinderID, nil
}
func (s *SQLiteDB) getGrindersByLastAdded(ctx context.Context, limit int) ([]grinder, error) {
grinders := make([]grinder, 0, limit)
if err := s.TransactContext(ctx, func(ctx context.Context, tx *sql.Tx) error {
rows, err := tx.QueryContext(ctx, `
SELECT name, company, max_grind_setting
FROM grinders
ORDER BY id DESC
LIMIT :limit
`,
sql.Named("limit", limit),
)
if err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to retrieve brewing method rows: %w", err)
}
defer rows.Close()
for rows.Next() {
var grinder grinder
var company, maxGrindSetting interface{}
if err := rows.Scan(&grinder.name, &company, &maxGrindSetting); err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to scan row: %w", err)
}
// Deal with possible NULL values
if v := reflect.ValueOf(company); v.Kind() == reflect.String {
grinder.company = company.(string)
} else {
grinder.company = "Unknown"
}
if v := reflect.ValueOf(maxGrindSetting); v.Kind() == reflect.Int64 {
grinder.maxGrindSetting = int(maxGrindSetting.(int64))
} else {
grinder.maxGrindSetting = -1
}
grinders = append(grinders, grinder)
}
if err := rows.Err(); err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to scan last row: %w", err)
}
return nil
}); err != nil {
return nil, fmt.Errorf("buna: sqlite_db_retrieve: getGrindersByLastAdded transaction failed: %w", err)
}
return grinders, nil
}
func (s *SQLiteDB) getMethodIDByName(ctx context.Context, name string) (int, error) {
var methodID int
if err := s.TransactContext(ctx, func(ctx context.Context, tx *sql.Tx) error {
if err := tx.QueryRowContext(ctx, `
SELECT id
FROM brewing_methods
WHERE name = :brewingMethodName
`,
sql.Named("brewingMethodName", name),
).Scan(&methodID); err != nil {
return fmt.Errorf("buna: sqlite_db_retrieve: failed to retrieve method id from db: %w", err)
}
return nil
}); err != nil {
return 0, fmt.Errorf("buna: sqlite_db_retrieve: getMethodIDByName transaction failed: %w", err)
}
return methodID, nil
}