-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_impl.go
139 lines (105 loc) · 3.6 KB
/
sql_impl.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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"time"
_ "github.com/lib/pq"
)
const (
loggedUsersTable = "logged_users"
attachmentsTable = "attachments"
monthCacheTable = "month_cache"
)
var cachedData *CachedData
type DBConfs struct {
user string
password string
host string
schema string
}
type CachedData struct {
db *sql.DB
}
func initCache(opts *DBConfs) error {
db, err := sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", opts.user, opts.password, opts.host, opts.schema))
if err != nil {
return err
}
// See "Important settings" section.
db.SetConnMaxLifetime(time.Minute * 3)
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
if err := validateTables(opts.schema, db); err != nil {
return err
}
cachedData = &CachedData{
db: db,
}
return nil
}
func (cd *CachedData) storeToken(user string, token string) error {
_, err := cd.db.Exec("INSERT INTO "+loggedUsersTable+"(\"user\", token, last_updated) VALUES($1, $2, $3) "+
"ON CONFLICT (\"user\") DO UPDATE SET token = EXCLUDED.token, last_updated = EXCLUDED.last_updated WHERE "+loggedUsersTable+".\"user\" = $1", user, token, time.Now())
return err
}
func (cd *CachedData) loadUserTokens() (map[string]string, error) {
var user, token string
tokens := make(map[string]string)
rows, err := cd.db.Query("SELECT \"user\", token FROM " + loggedUsersTable)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
err = rows.Scan(&user, &token)
if err != nil {
return nil, err
}
tokens[user] = token
}
return tokens, nil
}
func (cd *CachedData) attachmentExists(id string) []string {
var fname, contentType string
err := cd.db.QueryRow("SELECT fname, content_type FROM "+attachmentsTable+" WHERE att_id = $1", id).Scan(&fname, &contentType)
if err != nil {
return nil
}
return []string{fname, contentType}
}
func (cd *CachedData) saveAttachment(id string, name string, contentType string) error {
_, err := cd.db.Exec("INSERT INTO "+attachmentsTable+"(att_id, fname, content_type, last_updated) VALUES($1, $2, $3, $4)", id, name, contentType, time.Now())
return err
}
func (cd *CachedData) getCacheForUserLastUpdate(user string, start time.Time, end time.Time) (time.Time, error) {
var lastUpdated sql.NullTime
err := cd.db.QueryRow("SELECT last_updated FROM "+monthCacheTable+" WHERE \"user\" = $1 AND start = $2 AND \"end\" = $3", user, start, end).Scan(&lastUpdated)
if err != nil {
return time.Now(), err
}
return lastUpdated.Time, nil
}
func (cd *CachedData) saveCacheForUser(user string, start time.Time, end time.Time, cachedValues []interface{}) error {
jsonData, err := json.Marshal(cachedValues)
if err != nil {
return err
}
_, err = cd.db.Exec("INSERT INTO "+monthCacheTable+"(\"user\", start, \"end\", contents, last_updated) VALUES($1, $2, $3, $4, $5) "+
"ON CONFLICT (start,\"end\",\"user\") DO UPDATE SET contents = EXCLUDED.contents, last_updated = EXCLUDED.last_updated "+
"WHERE "+monthCacheTable+".\"user\" = $1 AND "+monthCacheTable+".start = $2 AND "+monthCacheTable+".\"end\" = $3", user, start, end, string(jsonData), time.Now())
return err
}
func (cd *CachedData) getUserCache(user string, start time.Time, end time.Time) ([]interface{}, error) {
var cachedValues []interface{}
var cachedValuesText string
err := cd.db.QueryRow("SELECT contents FROM "+monthCacheTable+" WHERE \"user\" = $1 AND start = $2 AND \"end\" = $3", user, start, end).Scan(&cachedValuesText)
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(cachedValuesText), &cachedValues)
if err != nil {
return nil, err
}
return cachedValues, nil
}