-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriters.go
165 lines (133 loc) · 3.65 KB
/
writers.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
158
159
160
161
162
163
164
165
package main
// -----------------------------------------------------------------------------
// Imports
// -----------------------------------------------------------------------------
import (
"bytes"
"database/sql"
"encoding/csv"
"encoding/json"
"errors"
"fmt"
_ "github.com/mattn/go-sqlite3"
"os"
"strings"
)
// -----------------------------------------------------------------------------
// ReportWriter
// -----------------------------------------------------------------------------
type ReportWriter interface {
Write(report Report, outputPath string) error
}
// -----------------------------------------------------------------------------
// ReportWriterCSV
// -----------------------------------------------------------------------------
type ReportWriterCSV struct {
}
func (self ReportWriterCSV) Write(report Report, outputPath string) error {
b := new(bytes.Buffer)
w := csv.NewWriter(b)
headersMap := make(map[string]bool)
for k := range report {
for kk := range report[k] {
headersMap[kk] = true
}
}
headers := make([]string, 0, len(headersMap))
for k := range headersMap {
headers = append(headers, k)
}
w.Write(headers)
for k := range report {
values := make([]string, 0, len(report[k]))
for _, kk := range headers {
if v, found := report[k][kk]; found {
values = append(values, fmt.Sprint(v))
}
}
w.Write(values)
}
w.Flush()
return os.WriteFile(outputPath, []byte(b.String()), 0644)
}
// -----------------------------------------------------------------------------
// ReportWriterJSON
// -----------------------------------------------------------------------------
type ReportWriterJSON struct {
}
func (self ReportWriterJSON) Write(report Report, outputPath string) error {
txs := make([]ReportRow, 0, len(report))
for _, tx := range report {
txs = append(txs, tx)
}
s, err := json.MarshalIndent(txs, "", "\t")
if err != nil {
return err
}
return os.WriteFile(outputPath, []byte(string(s)), 0644)
}
// -----------------------------------------------------------------------------
// ReportWriterSQLite
// -----------------------------------------------------------------------------
type ReportWriterSQLite struct {
}
func (self ReportWriterSQLite) Write(report Report, outputPath string) error {
os.Remove(outputPath)
db, err := sql.Open("sqlite3", outputPath)
if err != nil {
return err
}
defer db.Close()
_, err = db.Exec(`
CREATE TABLE users_report (
id INT NOT NULL,
sum INT NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE users_categories_report (
user_id INT NOT NULL,
category_name VARCHAR(255) NOT NULL,
sum INT NOT NULL,
PRIMARY KEY (user_id, category_name)
);
`)
if err != nil {
return err
}
for _, row := range report {
_, err = db.Exec(`
INSERT INTO users_report (id, sum)
VALUES (?, ?)
`, row["user_id"], row["sum"])
if err != nil {
return err
}
for kk, vv := range row {
if strings.HasPrefix(kk, "category_") {
_, err = db.Exec(`
INSERT INTO users_categories_report (user_id, category_name, sum)
VALUES (?, ?, ?)
`, row["user_id"], strings.TrimPrefix(kk, "category_"), vv)
if err != nil {
return err
}
}
}
}
return nil
}
// -----------------------------------------------------------------------------
// CreateReportWriter
// -----------------------------------------------------------------------------
func CreateReportWriter(format string) (ReportWriter, error) {
if "csv" == format {
return new(ReportWriterCSV), nil
}
if "json" == format {
return new(ReportWriterJSON), nil
}
if "sqlite" == format {
return new(ReportWriterSQLite), nil
}
return nil, errors.New("unknown format: " + format)
}