feat: Support model code generation for multi tables (#1836)

* Support model code generation for multi tables

* Format code

* Format code

Co-authored-by: anqiansong <anqiansong@bytedance.com>
This commit is contained in:
anqiansong
2022-04-28 10:01:04 +08:00
committed by GitHub
parent f7a60cdc24
commit cc09ab2aba
3 changed files with 73 additions and 11 deletions

View File

@@ -87,13 +87,51 @@ func MySqlDataSource(ctx *cli.Context) error {
pathx.RegisterGoctlHome(home)
}
pattern := strings.TrimSpace(ctx.String(flagTable))
tableValue := ctx.StringSlice(flagTable)
patterns := parseTableList(tableValue)
cfg, err := config.NewConfig(style)
if err != nil {
return err
}
return fromMysqlDataSource(url, pattern, dir, cfg, cache, idea)
return fromMysqlDataSource(url, dir, patterns, cfg, cache, idea)
}
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
@@ -162,14 +200,14 @@ func fromDDL(src, dir string, cfg *config.Config, cache, idea bool, database str
return nil
}
func fromMysqlDataSource(url, pattern, dir string, cfg *config.Config, cache, idea bool) error {
func fromMysqlDataSource(url, dir string, tablePat pattern, cfg *config.Config, cache, idea bool) error {
log := console.NewConsole(idea)
if len(url) == 0 {
log.Error("%v", "expected data source of mysql, but nothing found")
return nil
}
if len(pattern) == 0 {
if len(tablePat) == 0 {
log.Error("%v", "expected table or table globbing patterns, but nothing found")
return nil
}
@@ -191,12 +229,7 @@ func fromMysqlDataSource(url, pattern, dir string, cfg *config.Config, cache, id
matchTables := make(map[string]*model.Table)
for _, item := range tables {
match, err := filepath.Match(pattern, item)
if err != nil {
return err
}
if !match {
if !tablePat.Match(item) {
continue
}