-
Notifications
You must be signed in to change notification settings - Fork 1
/
repository.go
99 lines (76 loc) · 1.85 KB
/
repository.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
package main
import (
"context"
"database/sql"
_ "github.com/lib/pq"
)
type Repository interface {
FindShorthands(context.Context, int64) ([]Shorthand, error)
FindExtractors(context.Context, int64) ([]Extractor, error)
}
type repository struct {
db *sql.DB
}
func NewRepository(db *sql.DB) Repository {
return &repository{
db: db,
}
}
func (r *repository) FindExtractors(ctx context.Context, bookID int64) ([]Extractor, error) {
var extractors []Extractor
rows, err := r.db.QueryContext(ctx, "select label, match, data_type from extractors where book_id = $1", bookID)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var label string
var match string
var dataType DataType
if err := rows.Scan(&label, &match, &dataType); err != nil {
return nil, err
}
extractor := Extractor{
Label: label,
Match: match,
DataType: dataType,
}
extractors = append(extractors, extractor)
}
if err := rows.Err(); err != nil {
return nil, err
}
return extractors, nil
}
func (r *repository) FindShorthands(ctx context.Context, bookID int64) ([]Shorthand, error) {
var shorthands []Shorthand
rows, err := r.db.QueryContext(ctx, "select priority, expansion, match, text from shorthands where book_id = $1", bookID)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var priority int
var expansion string
var match sql.NullString
var text sql.NullString
if err := rows.Scan(&priority, &expansion, &match, &text); err != nil {
return nil, err
}
shorthand := Shorthand{
Priority: priority,
Expansion: expansion,
}
if match.Valid {
shorthand.Match = &match.String
}
if text.Valid {
shorthand.Text = &text.String
}
shorthands = append(shorthands, shorthand)
}
if err := rows.Err(); err != nil {
return nil, err
}
return shorthands, nil
}