|
@@ -1,15 +1,17 @@
|
|
package command
|
|
package command
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "errors"
|
|
"io/ioutil"
|
|
"io/ioutil"
|
|
"path/filepath"
|
|
"path/filepath"
|
|
"strings"
|
|
"strings"
|
|
|
|
|
|
- "github.com/tal-tech/go-zero/core/collection"
|
|
|
|
|
|
+ "github.com/go-sql-driver/mysql"
|
|
"github.com/tal-tech/go-zero/core/logx"
|
|
"github.com/tal-tech/go-zero/core/logx"
|
|
"github.com/tal-tech/go-zero/core/stores/sqlx"
|
|
"github.com/tal-tech/go-zero/core/stores/sqlx"
|
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/gen"
|
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/gen"
|
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/model"
|
|
"github.com/tal-tech/go-zero/tools/goctl/model/sql/model"
|
|
|
|
+ "github.com/tal-tech/go-zero/tools/goctl/model/sql/util"
|
|
"github.com/tal-tech/go-zero/tools/goctl/util/console"
|
|
"github.com/tal-tech/go-zero/tools/goctl/util/console"
|
|
"github.com/urfave/cli"
|
|
"github.com/urfave/cli"
|
|
)
|
|
)
|
|
@@ -29,16 +31,25 @@ func MysqlDDL(ctx *cli.Context) error {
|
|
cache := ctx.Bool(flagCache)
|
|
cache := ctx.Bool(flagCache)
|
|
idea := ctx.Bool(flagIdea)
|
|
idea := ctx.Bool(flagIdea)
|
|
log := console.NewConsole(idea)
|
|
log := console.NewConsole(idea)
|
|
- fileSrc, err := filepath.Abs(src)
|
|
|
|
- if err != nil {
|
|
|
|
- return err
|
|
|
|
|
|
+ src = strings.TrimSpace(src)
|
|
|
|
+ if len(src) == 0 {
|
|
|
|
+ return errors.New("expected path or path globbing patterns, but nothing found")
|
|
}
|
|
}
|
|
- data, err := ioutil.ReadFile(fileSrc)
|
|
|
|
|
|
+
|
|
|
|
+ files, err := util.MatchFiles(src)
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
- source := string(data)
|
|
|
|
- generator := gen.NewDefaultGenerator(source, dir, gen.WithConsoleOption(log))
|
|
|
|
|
|
+
|
|
|
|
+ var source []string
|
|
|
|
+ for _, file := range files {
|
|
|
|
+ data, err := ioutil.ReadFile(file)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+ source = append(source, string(data))
|
|
|
|
+ }
|
|
|
|
+ generator := gen.NewDefaultGenerator(strings.Join(source, "\n"), dir, gen.WithConsoleOption(log))
|
|
err = generator.Start(cache)
|
|
err = generator.Start(cache)
|
|
if err != nil {
|
|
if err != nil {
|
|
log.Error("%v", err)
|
|
log.Error("%v", err)
|
|
@@ -51,36 +62,63 @@ func MyDataSource(ctx *cli.Context) error {
|
|
dir := strings.TrimSpace(ctx.String(flagDir))
|
|
dir := strings.TrimSpace(ctx.String(flagDir))
|
|
cache := ctx.Bool(flagCache)
|
|
cache := ctx.Bool(flagCache)
|
|
idea := ctx.Bool(flagIdea)
|
|
idea := ctx.Bool(flagIdea)
|
|
- table := strings.TrimSpace(ctx.String(flagTable))
|
|
|
|
|
|
+ pattern := strings.TrimSpace(ctx.String(flagTable))
|
|
log := console.NewConsole(idea)
|
|
log := console.NewConsole(idea)
|
|
if len(url) == 0 {
|
|
if len(url) == 0 {
|
|
- log.Error("%v", "expected data source of mysql, but is empty")
|
|
|
|
|
|
+ log.Error("%v", "expected data source of mysql, but nothing found")
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
- if len(table) == 0 {
|
|
|
|
- log.Error("%v", "expected table(s), but nothing found")
|
|
|
|
|
|
+
|
|
|
|
+ if len(pattern) == 0 {
|
|
|
|
+ log.Error("%v", "expected table or table globbing patterns, but nothing found")
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ cfg, err := mysql.ParseDSN(url)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
logx.Disable()
|
|
logx.Disable()
|
|
conn := sqlx.NewMysql(url)
|
|
conn := sqlx.NewMysql(url)
|
|
|
|
+ databaseSource := strings.TrimSuffix(url, "/"+cfg.DBName) + "/information_schema"
|
|
|
|
+ db := sqlx.NewMysql(databaseSource)
|
|
m := model.NewDDLModel(conn)
|
|
m := model.NewDDLModel(conn)
|
|
- tables := collection.NewSet()
|
|
|
|
- for _, item := range strings.Split(table, ",") {
|
|
|
|
- item = strings.TrimSpace(item)
|
|
|
|
- if len(item) == 0 {
|
|
|
|
|
|
+ im := model.NewInformationSchemaModel(db)
|
|
|
|
+
|
|
|
|
+ tables, err := im.GetAllTables(cfg.DBName)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var matchTables []string
|
|
|
|
+ for _, item := range tables {
|
|
|
|
+ match, err := filepath.Match(pattern, item)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if !match {
|
|
continue
|
|
continue
|
|
}
|
|
}
|
|
- tables.AddStr(item)
|
|
|
|
|
|
+
|
|
|
|
+ matchTables = append(matchTables, item)
|
|
|
|
+ }
|
|
|
|
+ if len(matchTables) == 0 {
|
|
|
|
+ return errors.New("no tables matched")
|
|
}
|
|
}
|
|
- ddl, err := m.ShowDDL(tables.KeysStr()...)
|
|
|
|
|
|
+
|
|
|
|
+ ddl, err := m.ShowDDL(matchTables...)
|
|
if err != nil {
|
|
if err != nil {
|
|
log.Error("%v", err)
|
|
log.Error("%v", err)
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
+
|
|
generator := gen.NewDefaultGenerator(strings.Join(ddl, "\n"), dir, gen.WithConsoleOption(log))
|
|
generator := gen.NewDefaultGenerator(strings.Join(ddl, "\n"), dir, gen.WithConsoleOption(log))
|
|
err = generator.Start(cache)
|
|
err = generator.Start(cache)
|
|
if err != nil {
|
|
if err != nil {
|
|
log.Error("%v", err)
|
|
log.Error("%v", err)
|
|
}
|
|
}
|
|
|
|
+
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|