-
Notifications
You must be signed in to change notification settings - Fork 1
/
csvschema.go
178 lines (153 loc) · 3.41 KB
/
csvschema.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
169
170
171
172
173
174
175
176
177
178
package main
import (
"encoding/csv"
"fmt"
"io"
"os"
"path"
"regexp"
"strconv"
"strings"
"time"
)
var nonFriendlyCharacters = regexp.MustCompile(`[^a-zA-Z0-9_]+`)
var tooManyUnderscores = regexp.MustCompile(`_+`)
func friendlyName(name string) string {
friendly := strings.ToLower(name)
friendly = nonFriendlyCharacters.ReplaceAllString(friendly, "_")
friendly = tooManyUnderscores.ReplaceAllString(friendly, "_")
friendly = strings.Trim(friendly, "_")
return friendly
}
type fieldType struct {
Name string
Test func(string) bool
}
var types = []fieldType{
fieldType{
"timestamp",
func (value string) bool {
_, err := time.Parse("01/02/2006", value)
if err == nil {
return true
}
_, err = time.Parse(time.RFC3339, value)
return err == nil
},
},
fieldType{
"int",
func (value string) bool {
_, err := strconv.ParseInt(value, 10, 32)
return err == nil
},
},
fieldType{
"bigint",
func (value string) bool {
_, err := strconv.ParseInt(value, 10, 64)
return err == nil
},
},
fieldType{
"decimal",
func (value string) bool {
_, err := strconv.ParseFloat(value, 32)
return err == nil
},
},
fieldType{
"boolean",
func (value string) bool {
_, err := strconv.ParseBool(value)
return err == nil
},
},
}
type FieldInfo struct {
FieldName string
FieldType string
MaxLength int
}
func main() {
if len(os.Args) != 2 {
fmt.Printf("Usage: %s <csv>\n", os.Args[0])
os.Exit(1)
}
fileReader, err := os.Open(os.Args[1])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
_, filename := path.Split(os.Args[1])
fileParts := strings.Split(filename, ".")
tablename := strings.Join(fileParts[:len(fileParts)-1], ".")
csvReader := csv.NewReader(fileReader)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
columns, err := csvReader.Read()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
discoveredTypeIndexes := make([]int, len(columns))
discoveredMaxLengths := make([]int, len(columns))
fields := []FieldInfo{}
for {
row, err := csvReader.Read()
if err == io.EOF {
break
} else if err != nil {
fmt.Println(err)
os.Exit(1)
}
for fieldIndex, _ := range columns {
value := row[fieldIndex]
valueLength := len(value)
if discoveredMaxLengths[fieldIndex] < valueLength {
discoveredMaxLengths[fieldIndex] = valueLength
}
if discoveredTypeIndexes[fieldIndex] == len(types) {
continue
}
for {
if discoveredTypeIndexes[fieldIndex] == len(types) || value == "" {
break
}
match := types[discoveredTypeIndexes[fieldIndex]].Test(value)
if match == true {
break
}
discoveredTypeIndexes[fieldIndex] += 1
}
}
}
for fieldIndex, fieldName := range columns {
var fieldType string
if discoveredTypeIndexes[fieldIndex] == len(types) {
fieldType = "character varying"
} else {
fieldType = types[discoveredTypeIndexes[fieldIndex]].Name
}
fields = append(fields, FieldInfo{
friendlyName(fieldName),
fieldType,
discoveredMaxLengths[fieldIndex],
})
}
fmt.Println("CREATE TABLE \"" + tablename + "\" (")
for i, field := range fields {
if field.FieldType == "character varying" {
fmt.Printf(" \"" + field.FieldName + "\" " + field.FieldType + " (" + strconv.Itoa(field.MaxLength) + ")")
} else {
fmt.Printf(" \"" + field.FieldName + "\" " + field.FieldType)
}
if i < len(fields)-1 {
fmt.Printf(",")
}
fmt.Printf("\n")
}
fmt.Println(");")
}