-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount_group.go
77 lines (66 loc) · 1.94 KB
/
account_group.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
package api
import (
"database/sql"
"github.com/pkg/errors"
)
// AccountGroup represents an account group in a MoneyWell document. An account group correlates
// 1:1 with a record in the ZACCOUNTGROUP table. Not all columns are exported.
//
// The MoneyWell SQLite schema for the ZACCOUNTGROUP table is as follows:
// > .schema ZACCOUNTGROUP
// CREATE TABLE ZACCOUNTGROUP (
// Z_PK INTEGER PRIMARY KEY,
// Z_ENT INTEGER,
// Z_OPT INTEGER,
// ZSEQUENCE INTEGER,
// ZNAME VARCHAR,
// ZTICDSSYNCID VARCHAR,
// ZUNIQUEID VARCHAR
// );
type AccountGroup struct {
PrimaryKey int64
Name string
}
// GetAccountGroups fetches the set of accounts in a MoneyWell document, sorted by the display
// order as MoneyWell itself would render.
func GetAccountGroups(database *sql.DB) ([]AccountGroup, error) {
rows, err := database.Query(`
SELECT
zag.Z_PK,
zag.ZNAME
FROM
ZACCOUNTGROUP zag
ORDER BY
-- Sort by the account group sequence
zag.ZSEQUENCE ASC
`)
if err != nil {
return nil, errors.Wrap(err, "failed to query account groups")
}
defer rows.Close()
accountGroups := []AccountGroup{}
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 account group")
}
accountGroups = append(accountGroups, AccountGroup{
PrimaryKey: primaryKey,
Name: name,
})
}
return accountGroups, nil
}
func GetAccountGroupsMap(database *sql.DB) (map[int64]AccountGroup, error) {
accountGroups, err := GetAccountGroups(database)
if err != nil {
return nil, errors.WithStack(err)
}
accountGroupsMap := make(map[int64]AccountGroup, len(accountGroups))
for _, accountGroup := range accountGroups {
accountGroupsMap[accountGroup.PrimaryKey] = accountGroup
}
return accountGroupsMap, nil
}