-
Notifications
You must be signed in to change notification settings - Fork 9
/
export.go
168 lines (145 loc) · 3.49 KB
/
export.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
166
167
168
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/Valentin-Kaiser/go-dbase/dbase"
)
const path = "../test_data/database/EXPENSES.DBC"
const exportPath = "export/"
type DatabaseSchema struct {
Name string
Tables map[string]string
Generated time.Duration
}
type Table struct {
Name string
Columns uint16
Records uint32
FirstRow int
RowLength string
FileSize int64
Modified time.Time
Fields map[string]Field
Data []map[string]interface{}
}
type Field struct {
Name string
Type string
GoType string
Length int
}
func main() {
// Open debug log file so we see what's going on
f, err := os.OpenFile("debug.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(err)
return
}
dbase.Debug(true, io.MultiWriter(os.Stdout, f))
start := time.Now()
db, err := dbase.OpenDatabase(&dbase.Config{
Filename: path,
TrimSpaces: true,
})
if err != nil {
panic(err)
}
defer db.Close()
schema := db.Schema()
tables := db.Tables()
// length := len(tables)
databaseSchema := DatabaseSchema{
Name: strings.Trim(filepath.Base(path), filepath.Ext(path)),
Tables: make(map[string]string),
}
keys := make([]string, 0)
for table := range schema {
keys = append(keys, table)
}
sort.Strings(keys)
for it, tablename := range keys {
fmt.Printf("Exporting table %v (%v/%v)...\n", tablename, it+1, len(keys))
t := Table{
Name: strings.ToUpper(tablename),
Columns: tables[tablename].Header().ColumnsCount(),
Records: tables[tablename].Header().RecordsCount(),
FileSize: tables[tablename].Header().FileSize(),
Modified: tables[tablename].Header().Modified(0),
Fields: make(map[string]Field),
Data: make([]map[string]interface{}, 0),
}
for _, field := range schema[tablename] {
typ, err := field.Reflect()
if err != nil {
panic(err)
}
t.Fields[field.Name()] = Field{
Name: field.Name(),
Type: field.Type(),
GoType: typ.String(),
Length: int(field.Length),
}
}
// Print table data
rows, err := tables[tablename].Rows(true, true)
if err != nil {
panic(err)
}
for ir, row := range rows {
m, err := row.ToMap()
if err != nil {
panic(err)
}
t.Data = append(t.Data, m)
fmt.Printf("Exported %v/%v tables %v/%v records \n", it+1, len(keys), ir+1, t.Records)
}
// Write table to file
b, err := json.MarshalIndent(t, "", " ")
if err != nil {
panic(err)
}
err = os.WriteFile(fmt.Sprintf("%v%v.json", exportPath, t.Name), b, 0600)
if err != nil {
panic(err)
}
databaseSchema.Tables[strings.ToUpper(tablename)] = t.Name
fmt.Printf("Export %v/%v table completed \n", it+1, len(tables))
}
duration := time.Since(start)
databaseSchema.Generated = duration
// JSON encoding
b, err := json.MarshalIndent(databaseSchema, "", " ")
if err != nil {
fmt.Println(err)
return
}
// Open schema output file
schemaFile, err := os.OpenFile(fmt.Sprintf("%v%v.json", exportPath, databaseSchema.Name), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
panic(err)
}
_, err = schemaFile.Write(b)
if err != nil {
panic(err)
}
}
// ToByteString returns the number of bytes as a string with a unit
func ToByteString(b int) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB",
float64(b)/float64(div), "kMGTPE"[exp])
}