-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbqueries.go
82 lines (79 loc) · 1.8 KB
/
dbqueries.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
// /home/krylon/go/src/github.com/blicero/recovery/database/dbqueries.go
// -*- mode: go; coding: utf-8; -*-
// Created on 22. 03. 2022 by Benjamin Walkenhorst
// (c) 2022 Benjamin Walkenhorst
// Time-stamp: <2022-04-05 15:16:27 krylon>
package database
import "github.com/blicero/recovery/database/query"
var dbQueries = map[query.ID]string{
query.MoodAdd: "INSERT INTO mood (timestamp, score, note) VALUES (?, ?, ?)",
query.MoodGetByTime: `
SELECT
id,
timestamp,
score,
note
FROM mood
WHERE timestamp BETWEEN ? AND ?
ORDER BY timestamp
`,
query.MoodGetMostRecent: `
SELECT
id,
timestamp,
score,
note
FROM mood
ORDER BY timestamp DESC
LIMIT ?
`,
query.MoodGetRunningAverage: `
SELECT
m.id,
m.timestamp,
CAST(ROUND(AVG(score) FILTER (
WHERE timestamp BETWEEN m.timestamp - ?
AND m.timestamp)
OVER (ORDER BY timestamp)) AS INTEGER)
AS mavg,
m.note
FROM mood m
ORDER BY timestamp
LIMIT ?
`,
query.CravingAdd: "INSERT INTO craving (timestamp, score, note) VALUES (?, ?, ?)",
query.CravingGetByTime: `
SELECT
id,
timestamp,
score,
note
FROM craving
WHERE timestamp BETWEEN ? AND ?
ORDER BY timestamp
`,
query.CravingGetMostRecent: `
SELECT
id,
timestamp,
score,
note
FROM craving
ORDER BY timestamp DESC
LIMIT ?
`,
query.CravingGetRunningAverage: `
SELECT
c.id,
c.timestamp,
CAST(ROUND(AVG(score) FILTER (
WHERE timestamp BETWEEN c.timestamp - ?
AND c.timestamp)
OVER (ORDER BY timestamp)) AS INTEGER)
AS mavg,
c.note
FROM craving c
ORDER BY timestamp
LIMIT ?
`,
}