|
@@ -2,7 +2,6 @@ package command
|
|
|
|
|
|
import (
|
|
import (
|
|
"errors"
|
|
"errors"
|
|
- "fmt"
|
|
|
|
"io/ioutil"
|
|
"io/ioutil"
|
|
"path/filepath"
|
|
"path/filepath"
|
|
"strings"
|
|
"strings"
|
|
@@ -10,6 +9,7 @@ import (
|
|
"github.com/go-sql-driver/mysql"
|
|
"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/config"
|
|
"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/model/sql/util"
|
|
@@ -24,9 +24,9 @@ const (
|
|
flagDir = "dir"
|
|
flagDir = "dir"
|
|
flagCache = "cache"
|
|
flagCache = "cache"
|
|
flagIdea = "idea"
|
|
flagIdea = "idea"
|
|
- flagStyle = "style"
|
|
|
|
flagUrl = "url"
|
|
flagUrl = "url"
|
|
flagTable = "table"
|
|
flagTable = "table"
|
|
|
|
+ flagStyle = "style"
|
|
)
|
|
)
|
|
|
|
|
|
func MysqlDDL(ctx *cli.Context) error {
|
|
func MysqlDDL(ctx *cli.Context) error {
|
|
@@ -34,8 +34,13 @@ func MysqlDDL(ctx *cli.Context) error {
|
|
dir := ctx.String(flagDir)
|
|
dir := ctx.String(flagDir)
|
|
cache := ctx.Bool(flagCache)
|
|
cache := ctx.Bool(flagCache)
|
|
idea := ctx.Bool(flagIdea)
|
|
idea := ctx.Bool(flagIdea)
|
|
- namingStyle := strings.TrimSpace(ctx.String(flagStyle))
|
|
|
|
- return fromDDl(src, dir, namingStyle, cache, idea)
|
|
|
|
|
|
+ style := ctx.String(flagStyle)
|
|
|
|
+ cfg, err := config.NewConfig(style)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return fromDDl(src, dir, cfg, cache, idea)
|
|
}
|
|
}
|
|
|
|
|
|
func MyDataSource(ctx *cli.Context) error {
|
|
func MyDataSource(ctx *cli.Context) error {
|
|
@@ -43,26 +48,23 @@ 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)
|
|
- namingStyle := strings.TrimSpace(ctx.String(flagStyle))
|
|
|
|
|
|
+ style := ctx.String(flagStyle)
|
|
pattern := strings.TrimSpace(ctx.String(flagTable))
|
|
pattern := strings.TrimSpace(ctx.String(flagTable))
|
|
- return fromDataSource(url, pattern, dir, namingStyle, cache, idea)
|
|
|
|
|
|
+ cfg, err := config.NewConfig(style)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return fromDataSource(url, pattern, dir, cfg, cache, idea)
|
|
}
|
|
}
|
|
|
|
|
|
-func fromDDl(src, dir, namingStyle string, cache, idea bool) error {
|
|
|
|
|
|
+func fromDDl(src, dir string, cfg *config.Config, cache, idea bool) error {
|
|
log := console.NewConsole(idea)
|
|
log := console.NewConsole(idea)
|
|
src = strings.TrimSpace(src)
|
|
src = strings.TrimSpace(src)
|
|
if len(src) == 0 {
|
|
if len(src) == 0 {
|
|
return errors.New("expected path or path globbing patterns, but nothing found")
|
|
return errors.New("expected path or path globbing patterns, but nothing found")
|
|
}
|
|
}
|
|
|
|
|
|
- switch namingStyle {
|
|
|
|
- case gen.NamingLower, gen.NamingCamel, gen.NamingSnake:
|
|
|
|
- case "":
|
|
|
|
- namingStyle = gen.NamingLower
|
|
|
|
- default:
|
|
|
|
- return fmt.Errorf("unexpected naming style: %s", namingStyle)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
files, err := util.MatchFiles(src)
|
|
files, err := util.MatchFiles(src)
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
@@ -81,7 +83,7 @@ func fromDDl(src, dir, namingStyle string, cache, idea bool) error {
|
|
|
|
|
|
source = append(source, string(data))
|
|
source = append(source, string(data))
|
|
}
|
|
}
|
|
- generator, err := gen.NewDefaultGenerator(dir, namingStyle, gen.WithConsoleOption(log))
|
|
|
|
|
|
+ generator, err := gen.NewDefaultGenerator(dir, cfg, gen.WithConsoleOption(log))
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
@@ -90,7 +92,7 @@ func fromDDl(src, dir, namingStyle string, cache, idea bool) error {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
-func fromDataSource(url, pattern, dir, namingStyle string, cache, idea bool) error {
|
|
|
|
|
|
+func fromDataSource(url, pattern, dir string, cfg *config.Config, cache, idea bool) error {
|
|
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 nothing found")
|
|
log.Error("%v", "expected data source of mysql, but nothing found")
|
|
@@ -102,25 +104,17 @@ func fromDataSource(url, pattern, dir, namingStyle string, cache, idea bool) err
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
- switch namingStyle {
|
|
|
|
- case gen.NamingLower, gen.NamingCamel, gen.NamingSnake:
|
|
|
|
- case "":
|
|
|
|
- namingStyle = gen.NamingLower
|
|
|
|
- default:
|
|
|
|
- return fmt.Errorf("unexpected naming style: %s", namingStyle)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- cfg, err := mysql.ParseDSN(url)
|
|
|
|
|
|
+ dsn, err := mysql.ParseDSN(url)
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
logx.Disable()
|
|
logx.Disable()
|
|
- databaseSource := strings.TrimSuffix(url, "/"+cfg.DBName) + "/information_schema"
|
|
|
|
|
|
+ databaseSource := strings.TrimSuffix(url, "/"+dsn.DBName) + "/information_schema"
|
|
db := sqlx.NewMysql(databaseSource)
|
|
db := sqlx.NewMysql(databaseSource)
|
|
im := model.NewInformationSchemaModel(db)
|
|
im := model.NewInformationSchemaModel(db)
|
|
|
|
|
|
- tables, err := im.GetAllTables(cfg.DBName)
|
|
|
|
|
|
+ tables, err := im.GetAllTables(dsn.DBName)
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
@@ -135,7 +129,7 @@ func fromDataSource(url, pattern, dir, namingStyle string, cache, idea bool) err
|
|
if !match {
|
|
if !match {
|
|
continue
|
|
continue
|
|
}
|
|
}
|
|
- columns, err := im.FindByTableName(cfg.DBName, item)
|
|
|
|
|
|
+ columns, err := im.FindByTableName(dsn.DBName, item)
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
@@ -146,11 +140,11 @@ func fromDataSource(url, pattern, dir, namingStyle string, cache, idea bool) err
|
|
return errors.New("no tables matched")
|
|
return errors.New("no tables matched")
|
|
}
|
|
}
|
|
|
|
|
|
- generator, err := gen.NewDefaultGenerator(dir, namingStyle, gen.WithConsoleOption(log))
|
|
|
|
|
|
+ generator, err := gen.NewDefaultGenerator(dir, cfg, gen.WithConsoleOption(log))
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
- err = generator.StartFromInformationSchema(cfg.DBName, matchTables, cache)
|
|
|
|
|
|
+ err = generator.StartFromInformationSchema(dsn.DBName, matchTables, cache)
|
|
return err
|
|
return err
|
|
}
|
|
}
|