-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsettings.go
More file actions
72 lines (63 loc) · 1.82 KB
/
settings.go
File metadata and controls
72 lines (63 loc) · 1.82 KB
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
package api
import (
"database/sql"
"time"
"github.com/pkg/errors"
)
// Settings represents the single row from the ZETTINGS table in a MoneyWell document.
//
// The MoneyWell SQLite schema for the ZSETTINGS table is as follows:
// > .schema ZSETTINGS
// CREATE TABLE ZSETTINGS (
// Z_PK INTEGER PRIMARY KEY,
// Z_ENT INTEGER,
// Z_OPT INTEGER,
// ZCASHFLOWSTARTDATEYMD INTEGER,
// ZLASTFILLBUCKETSDATEYMD INTEGER,
// ZATTACHMENTPATH VARCHAR,
// ZTICDSSYNCID VARCHAR
// );
type Settings struct {
PrimaryKey int64
CashFlowStartDate time.Time
LastFillBucketsDate time.Time
AttachmentPath string
}
// GetSettings fetches the settings in a MoneyWell document.
func GetSettings(database *sql.DB) (Settings, error) {
row := database.QueryRow(`
SELECT
zs.Z_PK,
zs.ZCASHFLOWSTARTDATEYMD,
zs.ZLASTFILLBUCKETSDATEYMD,
zs.ZATTACHMENTPATH
FROM
ZSETTINGS zs
`)
var primaryKey int64
var cashFlowStartDateYMD, lastFillBucketsDateYMD int
var attachmentPath sql.NullString
err := row.Scan(
&primaryKey,
&cashFlowStartDateYMD,
&lastFillBucketsDateYMD,
&attachmentPath,
)
if err != nil {
return Settings{}, errors.Wrap(err, "failed to scan settings")
}
cashFlowStartDate, err := parseDateymd(cashFlowStartDateYMD)
if err != nil {
return Settings{}, errors.Wrapf(err, "failed to parse cash flow start date")
}
lastFillBucketsDate, err := parseDateymd(lastFillBucketsDateYMD)
if err != nil {
return Settings{}, errors.Wrapf(err, "failed to parse last fill buckets date")
}
return Settings{
PrimaryKey: primaryKey,
CashFlowStartDate: cashFlowStartDate,
LastFillBucketsDate: lastFillBucketsDate,
AttachmentPath: attachmentPath.String,
}, nil
}