forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
385 lines (335 loc) · 8.93 KB
/
command.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package command
import (
"errors"
"path/filepath"
"strings"
"github.com/go-sql-driver/mysql"
"github.com/spf13/cobra"
"github.com/zeromicro/go-zero/core/collection"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/postgres"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/model/sql/command/migrationnotes"
"github.com/zeromicro/go-zero/tools/goctl/model/sql/gen"
"github.com/zeromicro/go-zero/tools/goctl/model/sql/model"
"github.com/zeromicro/go-zero/tools/goctl/model/sql/util"
file "github.com/zeromicro/go-zero/tools/goctl/util"
"github.com/zeromicro/go-zero/tools/goctl/util/console"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
var (
// VarStringSrc describes the source file of sql.
VarStringSrc string
// VarStringDir describes the output directory of sql.
VarStringDir string
// VarBoolCache describes whether the cache is enabled.
VarBoolCache bool
// VarBoolIdea describes whether is idea or not.
VarBoolIdea bool
// VarStringURL describes the dsn of the sql.
VarStringURL string
// VarStringSliceTable describes tables.
VarStringSliceTable []string
// VarStringTable describes a table of sql.
VarStringTable string
// VarStringStyle describes the style.
VarStringStyle string
// VarStringDatabase describes the database.
VarStringDatabase string
// VarStringSchema describes the schema of postgresql.
VarStringSchema string
// VarStringHome describes the goctl home.
VarStringHome string
// VarStringRemote describes the remote git repository.
VarStringRemote string
// VarStringBranch describes the git branch of the repository.
VarStringBranch string
// VarBoolStrict describes whether the strict mode is enabled.
VarBoolStrict bool
// VarStringSliceIgnoreColumns represents the columns which are ignored.
VarStringSliceIgnoreColumns []string
)
var errNotMatched = errors.New("sql not matched")
// MysqlDDL generates model code from ddl
func MysqlDDL(_ *cobra.Command, _ []string) error {
migrationnotes.BeforeCommands(VarStringDir, VarStringStyle)
src := VarStringSrc
dir := VarStringDir
cache := VarBoolCache
idea := VarBoolIdea
style := VarStringStyle
database := VarStringDatabase
home := VarStringHome
remote := VarStringRemote
branch := VarStringBranch
if len(remote) > 0 {
repo, _ := file.CloneIntoGitHome(remote, branch)
if len(repo) > 0 {
home = repo
}
}
if len(home) > 0 {
pathx.RegisterGoctlHome(home)
}
cfg, err := config.NewConfig(style)
if err != nil {
return err
}
arg := ddlArg{
src: src,
dir: dir,
cfg: cfg,
cache: cache,
idea: idea,
database: database,
strict: VarBoolStrict,
ignoreColumns: mergeColumns(VarStringSliceIgnoreColumns),
}
return fromDDL(arg)
}
// MySqlDataSource generates model code from datasource
func MySqlDataSource(_ *cobra.Command, _ []string) error {
migrationnotes.BeforeCommands(VarStringDir, VarStringStyle)
url := strings.TrimSpace(VarStringURL)
dir := strings.TrimSpace(VarStringDir)
cache := VarBoolCache
idea := VarBoolIdea
style := VarStringStyle
home := VarStringHome
remote := VarStringRemote
branch := VarStringBranch
if len(remote) > 0 {
repo, _ := file.CloneIntoGitHome(remote, branch)
if len(repo) > 0 {
home = repo
}
}
if len(home) > 0 {
pathx.RegisterGoctlHome(home)
}
tableValue := VarStringSliceTable
patterns := parseTableList(tableValue)
cfg, err := config.NewConfig(style)
if err != nil {
return err
}
arg := dataSourceArg{
url: url,
dir: dir,
tablePat: patterns,
cfg: cfg,
cache: cache,
idea: idea,
strict: VarBoolStrict,
ignoreColumns: mergeColumns(VarStringSliceIgnoreColumns),
}
return fromMysqlDataSource(arg)
}
func mergeColumns(columns []string) []string {
set := collection.NewSet()
for _, v := range columns {
fields := strings.FieldsFunc(v, func(r rune) bool {
return r == ','
})
set.AddStr(fields...)
}
return set.KeysStr()
}
type pattern map[string]struct{}
func (p pattern) Match(s string) bool {
for v := range p {
match, err := filepath.Match(v, s)
if err != nil {
console.Error("%+v", err)
continue
}
if match {
return true
}
}
return false
}
func (p pattern) list() []string {
var ret []string
for v := range p {
ret = append(ret, v)
}
return ret
}
func parseTableList(tableValue []string) pattern {
tablePattern := make(pattern)
for _, v := range tableValue {
fields := strings.FieldsFunc(v, func(r rune) bool {
return r == ','
})
for _, f := range fields {
tablePattern[f] = struct{}{}
}
}
return tablePattern
}
// PostgreSqlDataSource generates model code from datasource
func PostgreSqlDataSource(_ *cobra.Command, _ []string) error {
migrationnotes.BeforeCommands(VarStringDir, VarStringStyle)
url := strings.TrimSpace(VarStringURL)
dir := strings.TrimSpace(VarStringDir)
cache := VarBoolCache
idea := VarBoolIdea
style := VarStringStyle
schema := VarStringSchema
home := VarStringHome
remote := VarStringRemote
branch := VarStringBranch
if len(remote) > 0 {
repo, _ := file.CloneIntoGitHome(remote, branch)
if len(repo) > 0 {
home = repo
}
}
if len(home) > 0 {
pathx.RegisterGoctlHome(home)
}
if len(schema) == 0 {
schema = "public"
}
pattern := strings.TrimSpace(VarStringTable)
cfg, err := config.NewConfig(style)
if err != nil {
return err
}
return fromPostgreSqlDataSource(url, pattern, dir, schema, cfg, cache, idea, VarBoolStrict)
}
type ddlArg struct {
src, dir string
cfg *config.Config
cache, idea bool
database string
strict bool
ignoreColumns []string
}
func fromDDL(arg ddlArg) error {
log := console.NewConsole(arg.idea)
src := strings.TrimSpace(arg.src)
if len(src) == 0 {
return errors.New("expected path or path globbing patterns, but nothing found")
}
files, err := util.MatchFiles(src)
if err != nil {
return err
}
if len(files) == 0 {
return errNotMatched
}
generator, err := gen.NewDefaultGenerator(arg.dir, arg.cfg,
gen.WithConsoleOption(log), gen.WithIgnoreColumns(arg.ignoreColumns))
if err != nil {
return err
}
for _, file := range files {
err = generator.StartFromDDL(file, arg.cache, arg.strict, arg.database)
if err != nil {
return err
}
}
return nil
}
type dataSourceArg struct {
url, dir string
tablePat pattern
cfg *config.Config
cache, idea bool
strict bool
ignoreColumns []string
}
func fromMysqlDataSource(arg dataSourceArg) error {
log := console.NewConsole(arg.idea)
if len(arg.url) == 0 {
log.Error("%v", "expected data source of mysql, but nothing found")
return nil
}
if len(arg.tablePat) == 0 {
log.Error("%v", "expected table or table globbing patterns, but nothing found")
return nil
}
dsn, err := mysql.ParseDSN(arg.url)
if err != nil {
return err
}
logx.Disable()
databaseSource := strings.TrimSuffix(arg.url, "/"+dsn.DBName) + "/information_schema"
db := sqlx.NewMysql(databaseSource)
im := model.NewInformationSchemaModel(db)
tables, err := im.GetAllTables(dsn.DBName)
if err != nil {
return err
}
matchTables := make(map[string]*model.Table)
for _, item := range tables {
if !arg.tablePat.Match(item) {
continue
}
columnData, err := im.FindColumns(dsn.DBName, item)
if err != nil {
return err
}
table, err := columnData.Convert()
if err != nil {
return err
}
matchTables[item] = table
}
if len(matchTables) == 0 {
return errors.New("no tables matched")
}
generator, err := gen.NewDefaultGenerator(arg.dir, arg.cfg,
gen.WithConsoleOption(log), gen.WithIgnoreColumns(arg.ignoreColumns))
if err != nil {
return err
}
return generator.StartFromInformationSchema(matchTables, arg.cache, arg.strict)
}
func fromPostgreSqlDataSource(url, pattern, dir, schema string, cfg *config.Config, cache, idea, strict bool) error {
log := console.NewConsole(idea)
if len(url) == 0 {
log.Error("%v", "expected data source of postgresql, but nothing found")
return nil
}
if len(pattern) == 0 {
log.Error("%v", "expected table or table globbing patterns, but nothing found")
return nil
}
db := postgres.New(url)
im := model.NewPostgreSqlModel(db)
tables, err := im.GetAllTables(schema)
if err != nil {
return err
}
matchTables := make(map[string]*model.Table)
for _, item := range tables {
match, err := filepath.Match(pattern, item)
if err != nil {
return err
}
if !match {
continue
}
columnData, err := im.FindColumns(schema, item)
if err != nil {
return err
}
table, err := columnData.Convert()
if err != nil {
return err
}
matchTables[item] = table
}
if len(matchTables) == 0 {
return errors.New("no tables matched")
}
generator, err := gen.NewDefaultGenerator(dir, cfg, gen.WithConsoleOption(log), gen.WithPostgreSql())
if err != nil {
return err
}
return generator.StartFromInformationSchema(matchTables, cache, strict)
}