-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathtable_last_access.go
47 lines (36 loc) · 1.17 KB
/
table_last_access.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
package database
import (
"database/sql"
"errors"
"github.com/t2bot/matrix-media-repo/common/rcontext"
)
type DbLastAccess struct {
Sha256Hash string
LastAccessTs int64
}
const upsertLastAccess = "INSERT INTO last_access (sha256_hash, last_access_ts) VALUES ($1, $2) ON CONFLICT (sha256_hash) DO UPDATE SET last_access_ts = $2;"
type lastAccessTableStatements struct {
upsertLastAccess *sql.Stmt
}
type lastAccessTableWithContext struct {
statements *lastAccessTableStatements
ctx rcontext.RequestContext
}
func prepareLastAccessTables(db *sql.DB) (*lastAccessTableStatements, error) {
var err error
var stmts = &lastAccessTableStatements{}
if stmts.upsertLastAccess, err = db.Prepare(upsertLastAccess); err != nil {
return nil, errors.New("error preparing upsertLastAccess: " + err.Error())
}
return stmts, nil
}
func (s *lastAccessTableStatements) Prepare(ctx rcontext.RequestContext) *lastAccessTableWithContext {
return &lastAccessTableWithContext{
statements: s,
ctx: ctx,
}
}
func (s *lastAccessTableWithContext) Upsert(sha256hash string, ts int64) error {
_, err := s.statements.upsertLastAccess.ExecContext(s.ctx, sha256hash, ts)
return err
}