Initial Commit
This commit is contained in:
59
generator.go
Normal file
59
generator.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
)
|
||||
|
||||
func generateCSV(outputFile string) error {
|
||||
columns, err := fetchTableMetadata()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error fetching table metadata: %w", err)
|
||||
}
|
||||
|
||||
file, err := os.Create(outputFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not create file: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
writer := csv.NewWriter(file)
|
||||
defer writer.Flush()
|
||||
|
||||
header := make([]string, len(columns))
|
||||
for i, col := range columns {
|
||||
header[i] = col.Name
|
||||
}
|
||||
|
||||
err = writer.Write(header)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error writing header: %w", err)
|
||||
}
|
||||
|
||||
for i := 0; i < numRows; i++ {
|
||||
rowData := make([]string, len(columns))
|
||||
for j, col := range columns {
|
||||
switch col.TypeName {
|
||||
case "integer", "float":
|
||||
rowData[j] = fmt.Sprint(rand.Intn(1000))
|
||||
case "varchar", "text":
|
||||
rowData[j] = fmt.Sprintf("sample text %d", rand.Intn(100))
|
||||
default:
|
||||
rowData[j] = ""
|
||||
}
|
||||
if col.MaxCharacterLength != nil && *col.MaxCharacterLength < len(rowData[j]) {
|
||||
rowData[j] = rowData[j][:*col.MaxCharacterLength]
|
||||
}
|
||||
}
|
||||
|
||||
err := writer.Write(rowData)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error writing row data: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("CSV file '%s' generated successfully\n", outputFile)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user