-
Notifications
You must be signed in to change notification settings - Fork 79
/
table_exports.go
68 lines (56 loc) · 1.89 KB
/
table_exports.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
package database
import (
"database/sql"
"errors"
"github.com/t2bot/matrix-media-repo/common/rcontext"
)
const insertExport = "INSERT INTO exports (export_id, entity) VALUES ($1, $2);"
const selectExportEntity = "SELECT entity FROM exports WHERE export_id = $1;"
const deleteExport = "DELETE FROM exports WHERE export_id = $1;"
type exportsTableStatements struct {
insertExport *sql.Stmt
selectExportEntity *sql.Stmt
deleteExport *sql.Stmt
}
type exportsTableWithContext struct {
statements *exportsTableStatements
ctx rcontext.RequestContext
}
func prepareExportsTables(db *sql.DB) (*exportsTableStatements, error) {
var err error
var stmts = &exportsTableStatements{}
if stmts.insertExport, err = db.Prepare(insertExport); err != nil {
return nil, errors.New("error preparing insertExport: " + err.Error())
}
if stmts.selectExportEntity, err = db.Prepare(selectExportEntity); err != nil {
return nil, errors.New("error preparing selectExportEntity: " + err.Error())
}
if stmts.deleteExport, err = db.Prepare(deleteExport); err != nil {
return nil, errors.New("error preparing deleteExport: " + err.Error())
}
return stmts, nil
}
func (s *exportsTableStatements) Prepare(ctx rcontext.RequestContext) *exportsTableWithContext {
return &exportsTableWithContext{
statements: s,
ctx: ctx,
}
}
func (s *exportsTableWithContext) Insert(exportId string, entity string) error {
_, err := s.statements.insertExport.ExecContext(s.ctx, exportId, entity)
return err
}
func (s *exportsTableWithContext) Delete(exportId string) error {
_, err := s.statements.deleteExport.ExecContext(s.ctx, exportId)
return err
}
func (s *exportsTableWithContext) GetEntity(exportId string) (string, error) {
row := s.statements.selectExportEntity.QueryRowContext(s.ctx, exportId)
val := ""
err := row.Scan(&val)
if errors.Is(err, sql.ErrNoRows) {
err = nil
val = ""
}
return val, err
}