-
Notifications
You must be signed in to change notification settings - Fork 0
/
entries.go
123 lines (102 loc) 路 2.64 KB
/
entries.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package client
import (
"database/sql"
"os"
"path"
sq "github.com/Masterminds/squirrel"
"github.com/cedricium/worklog"
_ "github.com/mattn/go-sqlite3"
)
type Entries struct {
Database *sql.DB
}
type ListConditions struct {
After string
Before string
Filters string
}
const (
dbFile string = "worklog.db"
initializeStmt string = `CREATE TABLE IF NOT EXISTS entries (
id TEXT NOT NULL PRIMARY KEY,
timestamp DATETIME NOT NULL,
important INTEGER NOT NULL DEFAULT 0,
category TEXT NOT NULL DEFAULT 'note',
message TEXT NOT NULL
);`
)
func (client *Entries) Initialize() error {
worklogDataDir := path.Join(os.Getenv("HOME"), ".local", "share", "worklog")
if _, err := os.Stat(worklogDataDir); os.IsNotExist(err) {
os.Mkdir(worklogDataDir, 0755)
}
db, err := sql.Open("sqlite3", path.Join(worklogDataDir, dbFile))
if err != nil {
return err
}
if _, err = db.Exec(initializeStmt); err != nil {
return err
}
client.Database = db
return nil
}
func (client *Entries) Add(entry worklog.Entry) error {
if _, err := sq.Insert("entries").Values(entry.ID, entry.Timestamp.Format(worklog.ISO8601),
entry.Important, entry.Category, entry.Message).RunWith(client.Database).Exec(); err != nil {
return err
}
return nil
}
func (client *Entries) List(entries *[]worklog.Entry, conds ListConditions) error {
exp := sq.Select("*").From("entries").OrderBy("timestamp DESC")
if len(conds.After) > 0 {
exp = exp.Where(sq.Gt{"timestamp": conds.After})
}
if len(conds.Before) > 0 {
exp = exp.Where(sq.Lt{"timestamp": conds.Before})
}
contains := []string{}
for _, char := range conds.Filters {
switch char {
// special case for filtering by `important` entries,
// irrelevant to proceeding category filtering
case 'I':
exp = exp.Where(sq.Eq{"important": true})
case 'B':
contains = append(contains, "bug")
case 'F':
contains = append(contains, "feature")
case 'R':
contains = append(contains, "fix")
case 'M':
contains = append(contains, "meeting")
case 'N':
contains = append(contains, "note")
case 'C':
contains = append(contains, "refactor")
}
}
if len(contains) > 0 {
exp = exp.Where(sq.Eq{"category": contains})
}
rows, err := exp.RunWith(client.Database).Query()
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
entry := worklog.Entry{}
if err := rows.Scan(&entry.ID, &entry.Timestamp, &entry.Important,
&entry.Category, &entry.Message); err != nil {
return err
}
*entries = append(*entries, entry)
}
return nil
}
func (client *Entries) Clear() error {
if _, err := sq.Delete("entries").RunWith(client.Database).Exec(); err != nil {
return err
}
return nil
}