-
Notifications
You must be signed in to change notification settings - Fork 35
/
history.go
128 lines (106 loc) · 2.52 KB
/
history.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
124
125
126
127
128
package main
import (
"path/filepath"
"time"
)
type HistoryItem struct {
Source string
Dest string
Timestamp int64
Id string
IntermediatePath string
}
func normalizePath(p string) string {
if p == "" {
return ""
}
output, err := filepath.Abs(filepath.Clean(p))
if err != nil {
panic(err)
}
return output
}
func clearHistory() error {
_, err := profileDb_.Exec("DELETE FROM history")
return err
}
func allHistoryItems() ([]HistoryItem, error) {
var output []HistoryItem
rows, err := profileDb_.Query("SELECT id, source, destination, timestamp FROM history ORDER BY id")
if err != nil {
return output, err
}
for rows.Next() {
var item HistoryItem
rows.Scan(&item.Id, &item.Source, &item.Dest, &item.Timestamp)
output = append(output, item)
}
return output, nil
}
func saveHistoryItems(fileActions []*FileAction) error {
if len(fileActions) == 0 {
return nil
}
tx, err := profileDb_.Begin()
if err != nil {
return err
}
for _, action := range fileActions {
if action.kind == KIND_DELETE {
// Current, undo is not supported
continue
}
tx.Exec("INSERT INTO history (source, destination, timestamp) VALUES (?, ?, ?)", action.FullOldPath(), action.FullNewPath(), time.Now().Unix())
}
return tx.Commit()
}
func deleteHistoryItems(items []HistoryItem) error {
if len(items) == 0 {
return nil
}
sqlOr := ""
for _, item := range items {
if sqlOr != "" {
sqlOr += " OR "
}
sqlOr += "id = " + item.Id
}
_, err := profileDb_.Exec("DELETE FROM history WHERE " + sqlOr)
return err
}
func deleteOldHistoryItems(minTimestamp int64) {
if profileDb_ != nil {
profileDb_.Exec("DELETE FROM history WHERE timestamp < ?", minTimestamp)
}
}
func latestHistoryItemsByDestinations(paths []string) ([]HistoryItem, error) {
var output []HistoryItem
if len(paths) == 0 {
return output, nil
}
sqlOr := ""
var sqlArgs []interface{}
for _, p := range paths {
sqlArgs = append(sqlArgs, p)
if sqlOr != "" {
sqlOr += " OR "
}
sqlOr += "destination = ?"
}
rows, err := profileDb_.Query("SELECT id, source, destination, timestamp FROM history WHERE "+sqlOr+" ORDER BY timestamp DESC", sqlArgs...)
if err != nil {
return output, err
}
doneDestinations := make(map[string]bool)
for rows.Next() {
var item HistoryItem
rows.Scan(&item.Id, &item.Source, &item.Dest, &item.Timestamp)
_, done := doneDestinations[item.Dest]
if done {
continue
}
output = append(output, item)
doneDestinations[item.Dest] = true
}
return output, nil
}