command.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package command
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "path/filepath"
  6. "strings"
  7. "github.com/go-sql-driver/mysql"
  8. "github.com/tal-tech/go-zero/core/logx"
  9. "github.com/tal-tech/go-zero/core/stores/sqlx"
  10. "github.com/tal-tech/go-zero/tools/goctl/config"
  11. "github.com/tal-tech/go-zero/tools/goctl/model/sql/gen"
  12. "github.com/tal-tech/go-zero/tools/goctl/model/sql/model"
  13. "github.com/tal-tech/go-zero/tools/goctl/model/sql/util"
  14. "github.com/tal-tech/go-zero/tools/goctl/util/console"
  15. "github.com/urfave/cli"
  16. )
  17. const (
  18. flagSrc = "src"
  19. flagDir = "dir"
  20. flagCache = "cache"
  21. flagIdea = "idea"
  22. flagUrl = "url"
  23. flagTable = "table"
  24. flagStyle = "style"
  25. )
  26. var errNotMatched = errors.New("sql not matched")
  27. func MysqlDDL(ctx *cli.Context) error {
  28. src := ctx.String(flagSrc)
  29. dir := ctx.String(flagDir)
  30. cache := ctx.Bool(flagCache)
  31. idea := ctx.Bool(flagIdea)
  32. style := ctx.String(flagStyle)
  33. cfg, err := config.NewConfig(style)
  34. if err != nil {
  35. return err
  36. }
  37. return fromDDl(src, dir, cfg, cache, idea)
  38. }
  39. func MyDataSource(ctx *cli.Context) error {
  40. url := strings.TrimSpace(ctx.String(flagUrl))
  41. dir := strings.TrimSpace(ctx.String(flagDir))
  42. cache := ctx.Bool(flagCache)
  43. idea := ctx.Bool(flagIdea)
  44. style := ctx.String(flagStyle)
  45. pattern := strings.TrimSpace(ctx.String(flagTable))
  46. cfg, err := config.NewConfig(style)
  47. if err != nil {
  48. return err
  49. }
  50. return fromDataSource(url, pattern, dir, cfg, cache, idea)
  51. }
  52. func fromDDl(src, dir string, cfg *config.Config, cache, idea bool) error {
  53. log := console.NewConsole(idea)
  54. src = strings.TrimSpace(src)
  55. if len(src) == 0 {
  56. return errors.New("expected path or path globbing patterns, but nothing found")
  57. }
  58. files, err := util.MatchFiles(src)
  59. if err != nil {
  60. return err
  61. }
  62. if len(files) == 0 {
  63. return errNotMatched
  64. }
  65. var source []string
  66. for _, file := range files {
  67. data, err := ioutil.ReadFile(file)
  68. if err != nil {
  69. return err
  70. }
  71. source = append(source, string(data))
  72. }
  73. generator, err := gen.NewDefaultGenerator(dir, cfg, gen.WithConsoleOption(log))
  74. if err != nil {
  75. return err
  76. }
  77. return generator.StartFromDDL(strings.Join(source, "\n"), cache)
  78. }
  79. func fromDataSource(url, pattern, dir string, cfg *config.Config, cache, idea bool) error {
  80. log := console.NewConsole(idea)
  81. if len(url) == 0 {
  82. log.Error("%v", "expected data source of mysql, but nothing found")
  83. return nil
  84. }
  85. if len(pattern) == 0 {
  86. log.Error("%v", "expected table or table globbing patterns, but nothing found")
  87. return nil
  88. }
  89. dsn, err := mysql.ParseDSN(url)
  90. if err != nil {
  91. return err
  92. }
  93. logx.Disable()
  94. databaseSource := strings.TrimSuffix(url, "/"+dsn.DBName) + "/information_schema"
  95. db := sqlx.NewMysql(databaseSource)
  96. im := model.NewInformationSchemaModel(db)
  97. tables, err := im.GetAllTables(dsn.DBName)
  98. if err != nil {
  99. return err
  100. }
  101. matchTables := make(map[string][]*model.Column)
  102. for _, item := range tables {
  103. match, err := filepath.Match(pattern, item)
  104. if err != nil {
  105. return err
  106. }
  107. if !match {
  108. continue
  109. }
  110. columns, err := im.FindByTableName(dsn.DBName, item)
  111. if err != nil {
  112. return err
  113. }
  114. matchTables[item] = columns
  115. }
  116. if len(matchTables) == 0 {
  117. return errors.New("no tables matched")
  118. }
  119. generator, err := gen.NewDefaultGenerator(dir, cfg, gen.WithConsoleOption(log))
  120. if err != nil {
  121. return err
  122. }
  123. return generator.StartFromInformationSchema(dsn.DBName, matchTables, cache)
  124. }