-
Notifications
You must be signed in to change notification settings - Fork 74
/
table_export_parts.go
101 lines (87 loc) · 3.4 KB
/
table_export_parts.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
package database
import (
"database/sql"
"errors"
"github.com/t2bot/matrix-media-repo/common/rcontext"
)
type DbExportPart struct {
ExportId string
PartNum int
SizeBytes int64
FileName string
DatastoreId string
Location string
}
const insertExportPart = "INSERT INTO export_parts (export_id, index, size_bytes, file_name, datastore_id, location) VALUES ($1, $2, $3, $4, $5, $6);"
const deleteExportPartsById = "DELETE FROM export_parts WHERE export_id = $1;"
const selectExportPartsById = "SELECT export_id, index, size_bytes, file_name, datastore_id, location FROM export_parts WHERE export_id = $1;"
const selectExportPartById = "SELECT export_id, index, size_bytes, file_name, datastore_id, location FROM export_parts WHERE export_id = $1 AND index = $2;"
type exportPartsTableStatements struct {
insertExportPart *sql.Stmt
deleteExportPartsById *sql.Stmt
selectExportPartsById *sql.Stmt
selectExportPartById *sql.Stmt
}
type exportPartsTableWithContext struct {
statements *exportPartsTableStatements
ctx rcontext.RequestContext
}
func prepareExportPartsTables(db *sql.DB) (*exportPartsTableStatements, error) {
var err error
var stmts = &exportPartsTableStatements{}
if stmts.insertExportPart, err = db.Prepare(insertExportPart); err != nil {
return nil, errors.New("error preparing insertExportPart: " + err.Error())
}
if stmts.deleteExportPartsById, err = db.Prepare(deleteExportPartsById); err != nil {
return nil, errors.New("error preparing deleteExportPartsById: " + err.Error())
}
if stmts.selectExportPartsById, err = db.Prepare(selectExportPartsById); err != nil {
return nil, errors.New("error preparing selectExportPartsById: " + err.Error())
}
if stmts.selectExportPartById, err = db.Prepare(selectExportPartById); err != nil {
return nil, errors.New("error preparing selectExportPartById: " + err.Error())
}
return stmts, nil
}
func (s *exportPartsTableStatements) Prepare(ctx rcontext.RequestContext) *exportPartsTableWithContext {
return &exportPartsTableWithContext{
statements: s,
ctx: ctx,
}
}
func (s *exportPartsTableWithContext) GetForExport(exportId string) ([]*DbExportPart, error) {
results := make([]*DbExportPart, 0)
rows, err := s.statements.selectExportPartsById.QueryContext(s.ctx, exportId)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return results, nil
}
return nil, err
}
for rows.Next() {
val := &DbExportPart{}
if err = rows.Scan(&val.ExportId, &val.PartNum, &val.SizeBytes, &val.FileName, &val.DatastoreId, &val.Location); err != nil {
return nil, err
}
results = append(results, val)
}
return results, nil
}
func (s *exportPartsTableWithContext) Get(exportId string, partNum int) (*DbExportPart, error) {
row := s.statements.selectExportPartById.QueryRowContext(s.ctx, exportId, partNum)
val := &DbExportPart{}
err := row.Scan(&val.ExportId, &val.PartNum, &val.SizeBytes, &val.FileName, &val.DatastoreId, &val.Location)
if errors.Is(err, sql.ErrNoRows) {
err = nil
val = nil
}
return val, err
}
func (s *exportPartsTableWithContext) Insert(part *DbExportPart) error {
_, err := s.statements.insertExportPart.ExecContext(s.ctx, part.ExportId, part.PartNum, part.SizeBytes, part.FileName, part.DatastoreId, part.Location)
return err
}
func (s *exportPartsTableWithContext) DeleteForExport(exportId string) error {
_, err := s.statements.deleteExportPartsById.ExecContext(s.ctx, exportId)
return err
}