-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmain.go
207 lines (185 loc) · 4.72 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"database/sql"
"encoding/json"
"errors"
"flag"
"fmt"
_ "github.com/go-sql-driver/mysql"
"log"
"os"
"strings"
)
var defaults = Configuration{
DbUser: "db_user",
DbPassword: "db_pw",
DbName: "bd_name",
PkgName: "DbStructs",
TagLabel: "db",
}
var config Configuration
type Configuration struct {
DbUser string `json:"db_user"`
DbPassword string `json:"db_password"`
DbName string `json:"db_name"`
// PkgName gives name of the package using the stucts
PkgName string `json:"pkg_name"`
// TagLabel produces tags commonly used to match database field names with Go struct members
TagLabel string `json:"tag_label"`
}
type ColumnSchema struct {
TableName string
ColumnName string
IsNullable string
DataType string
CharacterMaximumLength sql.NullInt64
NumericPrecision sql.NullInt64
NumericScale sql.NullInt64
ColumnType string
ColumnKey string
}
func writeStructs(schemas []ColumnSchema) (int, error) {
file, err := os.Create("db_structs.go")
if err != nil {
log.Fatal(err)
}
defer file.Close()
currentTable := ""
neededImports := make(map[string]bool)
// First, get body text into var out
out := ""
for _, cs := range schemas {
if cs.TableName != currentTable {
if currentTable != "" {
out = out + "}\n\n"
}
out = out + "type " + formatName(cs.TableName) + " struct{\n"
}
goType, requiredImport, err := goType(&cs)
if requiredImport != "" {
neededImports[requiredImport] = true
}
if err != nil {
log.Fatal(err)
}
out = out + "\t" + formatName(cs.ColumnName) + " " + goType
if len(config.TagLabel) > 0 {
out = out + "\t`" + config.TagLabel + ":\"" + cs.ColumnName + "\"`"
}
out = out + "\n"
currentTable = cs.TableName
}
out = out + "}"
// Now add the header section
header := "package " + config.PkgName + "\n\n"
if len(neededImports) > 0 {
header = header + "import (\n"
for imp := range neededImports {
header = header + "\t\"" + imp + "\"\n"
}
header = header + ")\n\n"
}
totalBytes, err := fmt.Fprint(file, header+out)
if err != nil {
log.Fatal(err)
}
return totalBytes, nil
}
func getSchema() []ColumnSchema {
conn, err := sql.Open("mysql", config.DbUser+":"+config.DbPassword+"@/information_schema")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
q := "SELECT TABLE_NAME, COLUMN_NAME, IS_NULLABLE, DATA_TYPE, " +
"CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, COLUMN_TYPE, " +
"COLUMN_KEY FROM COLUMNS WHERE TABLE_SCHEMA = ? ORDER BY TABLE_NAME, ORDINAL_POSITION"
rows, err := conn.Query(q, config.DbName)
if err != nil {
log.Fatal(err)
}
columns := []ColumnSchema{}
for rows.Next() {
cs := ColumnSchema{}
err := rows.Scan(&cs.TableName, &cs.ColumnName, &cs.IsNullable, &cs.DataType,
&cs.CharacterMaximumLength, &cs.NumericPrecision, &cs.NumericScale,
&cs.ColumnType, &cs.ColumnKey)
if err != nil {
log.Fatal(err)
}
columns = append(columns, cs)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
return columns
}
func formatName(name string) string {
parts := strings.Split(name, "_")
newName := ""
for _, p := range parts {
if len(p) < 1 {
continue
}
newName = newName + strings.Replace(p, string(p[0]), strings.ToUpper(string(p[0])), 1)
}
return newName
}
func goType(col *ColumnSchema) (string, string, error) {
requiredImport := ""
if col.IsNullable == "YES" {
requiredImport = "database/sql"
}
var gt string = ""
switch col.DataType {
case "char", "varchar", "enum", "set", "text", "longtext", "mediumtext", "tinytext":
if col.IsNullable == "YES" {
gt = "sql.NullString"
} else {
gt = "string"
}
case "blob", "mediumblob", "longblob", "varbinary", "binary":
gt = "[]byte"
case "date", "time", "datetime", "timestamp":
gt, requiredImport = "time.Time", "time"
case "bit", "tinyint", "smallint", "int", "mediumint", "bigint":
if col.IsNullable == "YES" {
gt = "sql.NullInt64"
} else {
gt = "int64"
}
case "float", "decimal", "double":
if col.IsNullable == "YES" {
gt = "sql.NullFloat64"
} else {
gt = "float64"
}
}
if gt == "" {
n := col.TableName + "." + col.ColumnName
return "", "", errors.New("No compatible datatype (" + col.DataType + ") for " + n + " found")
}
return gt, requiredImport, nil
}
var configFile = flag.String("json", "", "Config file")
func main() {
flag.Parse()
if len(*configFile) > 0 {
f, err := os.Open(*configFile)
if err != nil {
log.Fatal(err)
}
err = json.NewDecoder(f).Decode(&config)
if err != nil {
log.Fatal(err)
}
} else {
config = defaults
}
columns := getSchema()
bytes, err := writeStructs(columns)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Ok %d\n", bytes)
}