-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (95 loc) · 3.57 KB
/
main.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
package main
import (
"DBSeedr/dataGenerator"
"DBSeedr/dbaccess"
"database/sql"
"fmt"
"strings"
"sync"
)
func main() {
const tableToFillWithRandomData = "Addr"
//const tableToFillWithRandomData = "Person"
dbMetaData := getDbMetaData(tableToFillWithRandomData)
// Add any DB field name and value pairs that you want to fill with static data
// Example: non-nullable Foreign Keys will need to be in here, otherwise the insert will fail
staticFields := make(map[string]any)
//staticFields["state"] = "MN"
//staticFields["addressID"] = 1
var filteredMetaData []dbaccess.TableSchemaData
for idx, dbField := range dbMetaData {
if _, exists := staticFields[dbField.ColumnName]; !exists {
filteredMetaData = append(filteredMetaData, dbMetaData[idx])
}
}
var waitGroup sync.WaitGroup
for idx := 0; idx < 5; idx++ {
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
seedDb(tableToFillWithRandomData, staticFields, filteredMetaData)
}()
}
waitGroup.Wait()
}
// getDbMetaData uses the dbaccess package to get the database metadata and return the result.
func getDbMetaData(tableToFillWithRandomData string) []dbaccess.TableSchemaData {
dbMetaData, err := dbaccess.GetTableSchemaData(tableToFillWithRandomData)
if err != nil {
panic(err)
}
return dbMetaData
}
// seedDb will build and execute an insert query for the given table, database metadata, and any fields the user has chosen to fill on their own.
func seedDb(tableToFillWithRandomData string, staticFields map[string]any, dbMetaData []dbaccess.TableSchemaData) {
var sbQueryUpper, sbQueryLower, fullQuery strings.Builder
sbQueryUpper.WriteString("INSERT INTO dbo.")
sbQueryUpper.WriteString(tableToFillWithRandomData)
sbQueryUpper.WriteString("(")
sbQueryLower.WriteString(" VALUES (")
metaDataSliceLength := len(dbMetaData) - 1
var generatedVals []any
for idx, dbField := range dbMetaData {
_, fieldIsStaticallyFilled := staticFields[dbField.ColumnName]
// If the dbField is an identity field, skip it as it should get auto generated when a record is inserted
// If the dbField is in the list of staticFields, skip auto generating it
if !dbField.IsIdentity && !fieldIsStaticallyFilled {
sbQueryUpper.WriteString("[" + dbField.ColumnName + "]")
sbQueryLower.WriteString("?")
// if we are not done looping through metadata, add a comma before the next field and placeholder
if idx < metaDataSliceLength {
sbQueryUpper.WriteString(", ")
sbQueryLower.WriteString(", ")
}
generated, err := dataGenerator.GenerateRandomData(dbField.SystemDataType, dbField.MaxLength)
// Data could not be generated for the given type, append the NULL string to the generatedVals slice
if err != nil {
fmt.Println(err)
generatedVals = append(generatedVals, sql.NullString{})
}
// Data was generated, append it to the generatedVals slice
if err == nil {
generatedVals = append(generatedVals, generated)
}
}
}
// Add any staticFields records to the insert query
if totalStaticFields := len(staticFields); totalStaticFields > 0 {
insertLen := 0
for key, value := range staticFields {
if insertLen < totalStaticFields && len(generatedVals) > 0 {
sbQueryUpper.WriteString(", ")
sbQueryLower.WriteString(", ")
}
sbQueryUpper.WriteString("[" + key + "]")
sbQueryLower.WriteString("?")
generatedVals = append(generatedVals, value)
insertLen++
}
}
sbQueryUpper.WriteString(")")
sbQueryLower.WriteString(")")
fullQuery.WriteString(sbQueryUpper.String())
fullQuery.WriteString(sbQueryLower.String())
dbaccess.InsertGeneratedData(fullQuery.String(), generatedVals...)
}