-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
191 lines (157 loc) · 5.03 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
package main
import (
"database/sql"
"flag"
"os"
"strings"
_ "github.com/lib/pq"
"github.com/iancoleman/strcase"
"github.com/takuoki/gocase"
)
var (
sqlDriver = flag.String("dialect", "postgres", "database driver type (support only postgres)")
sqlSchema = flag.String("schema", "public", "database schema")
sqlConnStr = flag.String("conn", "host=localhost user=postgres dbname=postgres port=5432 sslmode=disable", "database connection string")
sqlDatabase = flag.String("d", "postgres", "database name")
outDir = flag.String("out", "model", "output directory")
pkgName = flag.String("pkg-name", "model", "go package name")
dbFile = flag.String("db-file", "db.go", "db implementation main file")
baseModelFile = flag.String("base-model-file", "base_model.go", "base model implementation file")
useBaseModel = flag.Bool("use-base-model-file", false, "use base model implementation")
customSuffix = flag.String("custom", "custom", "suffix used for custom file")
skipTables = flag.String("skip-tables", "db_migration_latest", "comma separated table list to skip")
omitJson = flag.String("omit-json", "created_at,updated_at,deleted_at", "comma separated columns list to omit json (set tag to '-')")
)
func main() {
flag.Parse()
skipTablesMap := make(map[string]bool)
for _, t := range strings.Split(*skipTables, ",") {
skipTablesMap[t] = true
}
omitJsonColumns := make(map[string]bool)
for _, t := range strings.Split(*omitJson, ",") {
omitJsonColumns[t] = true
}
dialect := postgresDialect{}
db, err := sql.Open(*sqlDriver, *sqlConnStr)
die(err)
defer db.Close()
tables, err := dialect.tables(db, *sqlSchema)
die(err)
var tableInfos []tableInfo
for _, name := range tables {
if skipTablesMap[name] {
continue
}
primaryKeys, err := dialect.primaryKeys(db, *sqlSchema, name)
die(err)
columns, err := dialect.columns(db, *sqlSchema, name)
die(err)
foreignTableNames, err := dialect.foreignTables(db, *sqlSchema, name)
die(err)
hasBaseModel := 0
if *useBaseModel {
for _, c := range columns {
if (c.Name == "id" && primaryKeys[c.Name]) ||
(c.Name == "updated_at" && goType(c) == TimeType) ||
(c.Name == "created_at" && goType(c) == TimeType) ||
(c.Name == "deleted_at" && goType(c) == TimeType) {
hasBaseModel++
}
}
}
ti := tableInfo{
Name: name,
FileName: name + ".go",
CustomFileName: name + "_" + *customSuffix + ".go",
PkgName: *pkgName,
StructName: filedName(name),
ShortStructName: argName(strcase.ToCamel(name))[:1],
HasPrimaryKey: len(primaryKeys) > 0,
HasManyPrimaryKey: len(primaryKeys) > 1,
HasBaseModel: *useBaseModel && hasBaseModel == 4,
}
for _, c := range columns {
goTyp := goType(c)
if goTyp == NotSupportedType {
continue
}
if primaryKeys[c.Name] {
ti.PrimaryKeys = append(ti.PrimaryKeys, primaryKeyInfo{
ColumnName: c.Name,
ArgName: argName(c.Name),
GoType: goTyp.String(),
})
}
ftn := foreignTableNames[c.Name]
ci := columnInfo{
Name: c.Name,
DBType: c.DataTypeName,
IsPrimaryKey: primaryKeys[c.Name],
FieldName: filedName(c.Name),
Nullable: c.Nullable,
GoType: goTyp.String(),
OmitJson: omitJsonColumns[c.Name],
}
// special case when file name == TableName.
// it's a reserved method for gorm interface.
if ci.FieldName == "TableName" {
ci.FieldName += "_"
}
if !ti.HasBaseModel || (ti.HasBaseModel && (c.Name != "id" &&
c.Name != "updated_at" &&
c.Name != "created_at" &&
c.Name != "deleted_at")) {
ti.Columns = append(ti.Columns, ci)
if pkgPath := pkgPath(goTyp); pkgPath != "" {
if isStandardPackages[pkgPath] {
ti.StdPkgs = append(ti.StdPkgs, pkgPath)
} else {
ti.NonStdPkgs = append(ti.NonStdPkgs, pkgPath)
}
}
}
if ftn != "" {
fieldName := strings.TrimSuffix(c.Name, "_id")
if fieldName == c.Name {
fieldName += "_rel"
}
ti.ForeignTables = append(ti.ForeignTables, foreignTableInfo{
FieldName: filedName(fieldName),
StructName: filedName(ftn),
Nullable: c.Nullable,
})
}
}
ti.HasForeignTable = len(ti.ForeignTables) > 0
ti.StdPkgs = uniqueStrings(ti.StdPkgs)
ti.NonStdPkgs = uniqueStrings(ti.NonStdPkgs)
tableInfos = append(tableInfos, ti)
}
die(os.MkdirAll(*outDir, 0755))
die(writeGoTmpl("db.go.tmpl", *dbFile, true, dbInfo{PkgName: *pkgName}))
if *useBaseModel {
die(writeGoTmpl("base_model.go.tmpl", *baseModelFile, true, dbInfo{PkgName: *pkgName}))
}
for _, ti := range tableInfos {
die(writeGoTmpl("model.go.tmpl", ti.FileName, true, ti))
die(writeGoTmpl("model_custom.go.tmpl", ti.CustomFileName, false, ti))
}
}
func filedName(name string) string {
if name == "type" {
name = "typ"
}
return gocase.To(strcase.ToCamel(name))
}
func argName(name string) string {
if name == "type" {
name = "typ"
}
return gocase.To(strcase.ToLowerCamel(name))
}
func die(err error) {
if err != nil {
panic(err)
}
}