-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag.go
74 lines (63 loc) · 1.54 KB
/
tag.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
package api
import (
"database/sql"
"github.com/pkg/errors"
)
// Tag represents a tag annotating a transaction in a MoneyWell document. A tag correlates 1:1
// with a record in the ZTAG table. Not all columns are exported.
//
// The MoneyWell SQLite schema for the ZTAG table is as follows:
// > .schema ZTAG
// CREATE TABLE ZTAG (
// Z_PK INTEGER PRIMARY KEY,
// Z_ENT INTEGER,
// Z_OPT INTEGER,
// ZNAME VARCHAR,
// ZTICDSSYNCID VARCHAR
// );
type Tag struct {
PrimaryKey int64
Name string
}
// GetTags fetches the set of tags in a MoneyWell document.
func GetTags(database *sql.DB) ([]Tag, error) {
rows, err := database.Query(`
SELECT
zt.Z_PK,
zt.ZNAME
FROM
ZTAG zt
ORDER BY
zt.ZNAME ASC
`)
if err != nil {
return nil, errors.Wrap(err, "failed to query tags")
}
defer rows.Close()
tags := []Tag{}
var primaryKey int64
var name string
for rows.Next() {
err := rows.Scan(&primaryKey, &name)
if err != nil {
return nil, errors.Wrap(err, "failed to scan tag")
}
tags = append(tags, Tag{
PrimaryKey: primaryKey,
Name: name,
})
}
return tags, nil
}
// GetTagsMap gets a map from the tag primary key to the tag.
func GetTagsMap(database *sql.DB) (map[int64]Tag, error) {
tags, err := GetTags(database)
if err != nil {
return nil, errors.WithStack(err)
}
tagsMap := make(map[int64]Tag, len(tags))
for _, tag := range tags {
tagsMap[tag.PrimaryKey] = tag
}
return tagsMap, nil
}