-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
157 lines (134 loc) · 3.17 KB
/
storage.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package storage
import (
"context"
"database/sql"
"errors"
"fmt"
"log"
"os"
"path/filepath"
)
const tempFile = "temp-config.txt"
const configDirName = "/.gcsv"
const configFilePath = "config.txt"
const separator = "="
const delimiter_key = "delimiter"
const favorite_key = "favorite"
type Storage struct {
Conn *sql.Conn
}
var storage *Storage
type FavoriteSheet struct {
Name string
ID string
}
func GetStorage() Storage {
if storage == nil {
storage = &Storage{
Conn: GetConnection(),
}
}
return *storage
}
func getDirPath() string {
dirname, err := os.UserHomeDir()
if err != nil {
log.Fatal("Not able to get user home dir", err)
}
return filepath.Join(dirname, configDirName)
}
func (s *Storage) SetProp(prop string, value string) error {
stmt := fmt.Sprintf(`
UPDATE config SET property_value = '%s' WHERE property_key = '%s';
INSERT INTO config (property_key, property_value)
SELECT '%s', '%s'
WHERE (Select Changes() = 0);
`, value, prop, prop, value)
_, err := s.Conn.ExecContext(context.Background(), stmt)
return err
}
func (s *Storage) GetDelimiter() string {
del, err := s.GetProp(delimiter_key)
if err != nil {
return ","
}
return del
}
func (s *Storage) GetSelectedFavorite() string {
favorite, err := s.GetProp(favorite_key)
if err != nil {
log.Fatal("No favorite selected")
}
return favorite
}
func (s *Storage) SetDelimiter(delimiter string) {
s.SetProp(delimiter_key, delimiter)
}
func (s *Storage) SetSelectedFavorite(name string) {
s.SetProp(favorite_key, name)
}
func (s *Storage) GetProp(prop string) (string, error) {
query := fmt.Sprintf(`
SELECT property_value FROM config WHERE property_key = '%s';
`, prop)
res, err := s.Conn.QueryContext(context.Background(), query)
if err != nil {
return "", err
}
if res.Next() == true {
var result string
res.Scan(&result)
return result, nil
}
return "", errors.New("Prop not found")
}
func (s *Storage) Store(sheet FavoriteSheet) {
query := `
INSERT INTO favorites(name, sheetId) VALUES('%s','%s');
`
fmtInsert := fmt.Sprintf(query, sheet.Name, sheet.ID)
_, err := s.Conn.ExecContext(context.Background(), fmtInsert)
if err != nil {
log.Fatal(err)
}
log.Println(fmt.Sprintf("Saved %s with ID: %s", sheet.Name, sheet.ID))
}
func (s *Storage) Remove(id string) error {
fmtQuery := fmt.Sprintf(`
DELETE FROM favorites WHERE name = '%s';
`, id)
_, err := s.Conn.ExecContext(context.Background(), fmtQuery)
return err
}
func (s *Storage) ListAll() []FavoriteSheet {
list := make([]FavoriteSheet, 0)
query := `
SELECT name, sheetId FROM favorites;
`
res, err := s.Conn.QueryContext(context.Background(), query)
if err != nil {
log.Fatal(err)
}
for res.Next() == true {
var ref FavoriteSheet
res.Scan(&ref.Name, &ref.ID)
list = append(list, ref)
}
return list
}
func (s *Storage) Get(name string) (string, error) {
query := fmt.Sprintf(`
SELECT sheetId FROM favorites WHERE name = '%s';
`, name)
res, err := s.Conn.QueryContext(context.Background(), query)
if err != nil {
return "", err
}
if res.Next() == true {
var id string
res.Scan(&id)
return id, nil
} else {
return "", errors.New(fmt.Sprintf("Favorite Sheet '%s' Not Found", name))
}
}