command.go 3.4 KB

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