Repository navigation
Expand file tree
/
Copy pathexport.go
More file actions
188 lines (167 loc) · 4.58 KB
/
Copy pathexport.go
File metadata and controls
188 lines (167 loc) · 4.58 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"os"
"time"
"github.com/brianvoe/gofakeit/v7"
)
// UserRecord is the structured export type for JSON marshalling
type UserRecord struct {
ID int `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
Phone string `json:"phone"`
Company string `json:"company"`
JobTitle string `json:"job_title"`
Address string `json:"address"`
City string `json:"city"`
Country string `json:"country"`
CreatedAt time.Time `json:"created_at"`
Status string `json:"status"`
}
// runExport generates mock data directly to a file in the requested format
func runExport(format string, rowCount int, outputPath string) error {
f, err := os.Create(outputPath)
if err != nil {
return fmt.Errorf("failed to create output file: %w", err)
}
defer f.Close()
faker := gofakeit.New(uint64(time.Now().UnixNano()))
switch format {
case "csv":
return exportCSV(f, rowCount, faker)
case "json":
return exportJSON(f, rowCount, faker)
case "sql":
return exportSQL(f, rowCount, faker)
default:
return fmt.Errorf("unsupported format: %q (use csv, json, or sql)", format)
}
}
func exportCSV(w *os.File, rowCount int, faker *gofakeit.Faker) error {
writer := csv.NewWriter(w)
defer writer.Flush()
header := []string{"id", "first_name", "last_name", "email", "phone", "company", "job_title", "address", "city", "country", "created_at", "status"}
if err := writer.Write(header); err != nil {
return err
}
for i := 0; i < rowCount; i++ {
row := generateUserRow(i, faker)
createdAt := row[10].(time.Time).Format("2006-01-02 15:04:05")
record := []string{
fmt.Sprintf("%d", row[0].(int)),
row[1].(string),
row[2].(string),
row[3].(string),
row[4].(string),
row[5].(string),
row[6].(string),
row[7].(string),
row[8].(string),
row[9].(string),
createdAt,
row[11].(string),
}
if err := writer.Write(record); err != nil {
return err
}
}
return nil
}
func exportJSON(w *os.File, rowCount int, faker *gofakeit.Faker) error {
encoder := json.NewEncoder(w)
for i := 0; i < rowCount; i++ {
row := generateUserRow(i, faker)
record := UserRecord{
ID: row[0].(int),
FirstName: row[1].(string),
LastName: row[2].(string),
Email: row[3].(string),
Phone: row[4].(string),
Company: row[5].(string),
JobTitle: row[6].(string),
Address: row[7].(string),
City: row[8].(string),
Country: row[9].(string),
CreatedAt: row[10].(time.Time),
Status: row[11].(string),
}
if err := encoder.Encode(record); err != nil {
return err
}
}
return nil
}
func exportSQL(w *os.File, rowCount int, faker *gofakeit.Faker) error {
createTable := `CREATE TABLE IF NOT EXISTS users (
id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(150),
phone VARCHAR(20),
company VARCHAR(100),
job_title VARCHAR(100),
address VARCHAR(200),
city VARCHAR(100),
country VARCHAR(100),
created_at TIMESTAMP,
status VARCHAR(20)
);
`
if _, err := w.WriteString(createTable); err != nil {
return err
}
// Multi-row INSERTs in batches of 1000 for performance
batchSize := 1000
for batchStart := 0; batchStart < rowCount; batchStart += batchSize {
batchEnd := batchStart + batchSize
if batchEnd > rowCount {
batchEnd = rowCount
}
if _, err := w.WriteString("INSERT INTO users (id, first_name, last_name, email, phone, company, job_title, address, city, country, created_at, status) VALUES\n"); err != nil {
return err
}
for i := batchStart; i < batchEnd; i++ {
row := generateUserRow(i, faker)
createdAt := row[10].(time.Time).Format("2006-01-02 15:04:05")
line := fmt.Sprintf("(%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
row[0].(int),
escapeSQL(row[1].(string)),
escapeSQL(row[2].(string)),
escapeSQL(row[3].(string)),
escapeSQL(row[4].(string)),
escapeSQL(row[5].(string)),
escapeSQL(row[6].(string)),
escapeSQL(row[7].(string)),
escapeSQL(row[8].(string)),
escapeSQL(row[9].(string)),
createdAt,
row[11].(string),
)
if i < batchEnd-1 {
line += ",\n"
} else {
line += ";\n\n"
}
if _, err := w.WriteString(line); err != nil {
return err
}
}
}
return nil
}
// escapeSQL doubles single quotes for safe SQL string literals
func escapeSQL(s string) string {
result := ""
for _, r := range s {
if r == '\'' {
result += "''"
} else {
result += string(r)
}
}
return result
}